Store plot values in a variable and then plot it

8 views (last 30 days)
I am trying to save the values of the first figure in a variable and then plot it.
But as you can see my code doesn't plot the same thing in the first figure and in the second one.
I don't know if plotdata.XData(index) and plotdata.YData is what doesn't work or if it is the greiddedInterpolant.
M=200; %Slope of my lines
x=linspace(0,M,127);
y=linspace(0,1,127);
x1=linspace(M,255,127);
y1=linspace(1,0,127);
hold on
plotdata=plot(x,y)
plotdata1=plot(x1,y1);
hold off
[~, index] = sort(plotdata.XData);
F = griddedInterpolant(plotdata.XData(index), plotdata.YData(index));
[~, index] = sort(plotdata1.XData);
F1 = griddedInterpolant(plotdata1.XData(index), plotdata1.YData(index));
F2=[F.Values F1.Values];
figure
plot(F2)

Accepted Answer

Simon Chan
Simon Chan on 17 Mar 2022
F.Values and F1.Values are those y-values only and hence the plot does not have any information about the corresponding x-values.
The x-value are stored in F.GridVectors and F1.GridVectors so modifying the last line is able to plot the 2nd figure correctly,
M=200; %Slope of my lines
x=linspace(0,M,127);
y=linspace(0,1,127);
x1=linspace(M,255,127);
y1=linspace(1,0,127);
plotdata=plot(x,y);
hold on
plotdata1=plot(x1,y1);
hold off
[~, index] = sort(plotdata.XData);
F = griddedInterpolant(plotdata.XData(index), plotdata.YData(index));
[~, index] = sort(plotdata1.XData);
F1 = griddedInterpolant(plotdata1.XData(index), plotdata1.YData(index));
figure
plot([F.GridVectors{1},F1.GridVectors{1}], [F.Values, F1.Values]);

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!