![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/569504/image.png)
How to plot arbitrary line slice against x or y axis in a table?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Biswajit Datta
am 29 Mär. 2021
Kommentiert: Biswajit Datta
am 12 Apr. 2021
I have a data in a regular grid with x and y axis. I would like to extract any line slice of the data taken along x axis (in between two user defined y values) and plot it with respect to the appropriate x axis.
data = rand(5, 5);
x_axis = [5 6 7 8 9];
y_axis = [10.1 10.2 10.3 10.4 10.5]';
%Adding the x and y axis to the data
dataWithAxes = array2table(data,'VariableNames',string(x_axis), 'RowNames',string(y_axis));
% slicing the data for y = 10.1 in between x = 5 and x = 8
x_start='5'; x_end='8';
[X,Y] = ismember({x_start,x_end},dataWithAxes.Properties.VariableNames);
sliced_data = dataWithAxes('10.1',Y(1):Y(2))
How do I plot the sliced_data with its x values (which is [5 6 7 8])? I do not want to call the variable 'x_axis' as the axis information is already there in the variable 'sliced_data' and 'dataWithAxes'. Please suggest a soultion. Thank you.
0 Kommentare
Akzeptierte Antwort
Pavan Guntha
am 1 Apr. 2021
Hi Biswajit,
You could use the VariableNames property of the table to get the column names which are actually the x axis data and then plot the data in the table using appropriate datatype conversions as follows:
xvars = sliced_data.Properties.VariableNames;
xData = cellfun(@str2double, xvars);
plot(xData,cell2mat(table2cell(sliced_data("10.1",:))),'*b')
grid
You could refer to the documentations of cellfun, table2cell and cell2mat for more details. The plot for the illustrated code is as follows:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/569504/image.png)
Hope this helps!
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Axis Labels finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!