plot not saveable, if it displays multiple functions

Hey there,
I am relatively new to Matlab and would greatly appreciate your help!
I am trying to save a plot of data I have collected (I have added an example of my script below). This works if I only plot one string of data, but as soon as I add a second one, the error
Conversion to matlab.graphics.chart.primitive.Line from cell is not possible.
Error in saveas (line 75)
h(n) = hP;
appears. Do I need to somehow configurate the plot, so that it is saveable?
My script:
%%load stored data
matfile1 = 'filedirectory1';
load(matfile1);
x1 = X;
y1 = Y;
matfile2 = 'filedirectory2';
load(matfile2);
x2 = X;
y2 = Y;
%%create Multiplot
image = plot(x1,y1,'b.',x2,y2,'r.');
%%save plot
saveas(image, 'filedirectory/image.png', 'png' );

 採用された回答

Jan
Jan 2018 年 6 月 19 日

0 投票

plot replies the handles of the line object, but saveas needs the handle of the figure:
FigH = figure; % Before the code
...
saveas(FigH, 'filedirectory/image.png', 'png' );

その他の回答 (2 件)

Guillaume
Guillaume 2018 年 6 月 19 日
編集済み: Guillaume 2018 年 6 月 19 日

1 投票

image = plot(...
plot does not return an image so your variable name is very misleading. It returns Line objects. I'm suprised that saveas works when you pass it a scalar Line object. Clearly it doesn't work with an array of Line objects but it shouldn't even work with a scalar. saveas expects a figure handle.
saveas(gcf, 'filedirectory/image.png', 'png' );
And for clarity you should change the plot line to:
hline = plot(...
although since you don't use the output, you could dispence with the variable altogether.
Larissa Billig
Larissa Billig 2018 年 6 月 19 日

0 投票

Thanks for your help!

カテゴリ

ヘルプ センター および File ExchangePrinting and Saving についてさらに検索

タグ

質問済み:

2018 年 6 月 19 日

回答済み:

2018 年 6 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by