MATLAB Answers

How to plot arbitrary line slice against x or y axis in a table?

1 view (last 30 days)
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.

Accepted Answer

Pavan Guntha
Pavan Guntha on 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:
Hope this helps!

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!