![](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 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Biswajit Datta
le 29 Mar 2021
Commenté : Biswajit Datta
le 12 Avr 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 commentaires
Réponse acceptée
Pavan Guntha
le 1 Avr 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!
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Axis Labels dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!