MATLAB Answers

Error in saveas, Invalid figure handle

28 ビュー (過去 30 日間)
Daphne PARLIARI
Daphne PARLIARI 2020 年 1 月 16 日
コメント済み: Star Strider 2020 年 1 月 17 日
Hello!
I am writing a code (see attached) which is turning out to be a bit long... and when I try to produce many plots I get the error
Error using saveas (line 83)
Invalid figure handle.
I suppose this is happening because the system gets overwhelmed by the many plots, right?
Is there a way to fix it but still get all the graphs I need?
Thank you!

  0 件のコメント

サインイン to comment.

採用された回答

Star Strider
Star Strider 2020 年 1 月 16 日
I cannot run your code because I do not have the necessary files.
However:
%% Let's plot T and RH timeseries
hf1 = figure;
idx = A.ToutoC <= 45 & A.ToutoC >= -10;
fig = plot(A.dec_time(idx), A.ToutoC(idx));
ylabel([vars{1},' (',varunits{1},')'],'fontsize',12,'fontweight','bold')
xlabel('Date')
title(['Measured ', vars{1},' for station ', namestr, ' 2015'])
out_file=[output_path,'\',namestr,'\',strrep(vars{1},' ','_'), '\T_plot_2015.jpg'];
saveas(fig,out_file)
(I added ‘hf1’ for this illustration.) Here, ‘fig’ is a handle to a line object, not a figure object, and ‘hf1’ is a handle to the figure object.
Consider the differences between ‘hf1’ and ‘fig’.
Yes, it¹s complicated. So is everything else you will ever encounter!

  2 件のコメント

Daphne PARLIARI
Daphne PARLIARI 2020 年 1 月 17 日
Thank you for your answer!
I am attaching 4 files ( I have many more to access) in order for you to understand the process I am trying to achieve.
Can you please explain what to do with hf1, besides adding one line to the code as you wrote above?
Shouldn't it be something like this?
%% Let's plot T and RH timeseries
hf1 = figure;
idx = A.ToutoC <= 45 & A.ToutoC >= -10;
figure = plot(A.dec_time(idx), A.ToutoC(idx));
ylabel([vars{1},' (',varunits{1},')'],'fontsize',12,'fontweight','bold')
xlabel('Date')
title(['Measured ', vars{1},' for station ', namestr, ' 2015'])
out_file=[output_path,'\',namestr,'\',strrep(vars{1},' ','_'), '\T_plot_2015.jpg'];
saveas(hf1,out_file)
Star Strider
Star Strider 2020 年 1 月 17 日
Shouldn't it be something like this?
Yes, it should. My apologies for the error.
Giving saveas (or savefig) the correct figure handle should work.

サインイン to comment.

その他の回答 (1 件)

Geoff Hayes
Geoff Hayes 2020 年 1 月 16 日
Daphne - from saveas, the first input parameter is a handle to the figure. You've named this fig in your code, but it seems to be the handle to the plot graphics object instead
for k = 1:365
index = doy == k; %An to doy isoutai me k, tote o index einai alithis (=1). Pseudis=0
B(k,1) = k;
B(k,2) = mean(A.ToutoC(index));
fig = plot(B(:,1),B(:,2)); % <------ fig is a handle to the plot graphics object
ylabel([vars{1},' (',varunits{1},')'],'fontsize',12,'fontweight','bold')
xlabel( 'Day of year')
title(['Mean daily ', vars{1},' for station ', namestr, ' 2015'])
out_file=[output_path,'\',namestr,'\',strrep(vars{1},' ','_'), '\Mean_Daily_T_2015.jpg'];
saveas(fig,out_file)
end
Try using
saveas(gcf,out_file)
where gcf is the current figure handle.
Out of curiosity, do you mean to be creating a file on each iteration of the loop? Your file name seems to be the same on every iteration so you would just be overwriting the file each time you call saveas. Perhaps you want to just write to file after all 365 iterations have been completed? If so, see hold which will retain the current plot (if that is what you need or want to do).

  2 件のコメント

Daphne PARLIARI
Daphne PARLIARI 2020 年 1 月 17 日
Good morning, thank you for your answer.
Please see attached files:
.xlsx is the input file I must do things with and .jpg is the plot I am producing with the part of code you mentioned above. I want mean daily values for the entire 2015, that's why I used k = 1:365.
Do you still think that it works in a false way?
Daphne PARLIARI
Daphne PARLIARI 2020 年 1 月 17 日
I just tried
% Daily mean T
for k = 1:365
index = doy == k;
B(k,1) = k;
B(k,2) = mean(A.ToutoC(index));
fig = plot(B(:,1),B(:,2));
ylabel([vars{1},' (',varunits{1},')'],'fontsize',12,'fontweight','bold')
xlabel( 'Day of year')
title(['Mean daily ', vars{1},' for station ', namestr, ' 2015'])
hold on
end
out_file=[output_path,'\',namestr,'\',strrep(vars{1},' ','_'), '\Mean_Daily_T_2015.jpg'];
saveas(fig,out_file)
hold off
and it returns the same plot as before but in much less time. But if I use hold on/off to the other daily mean plots, too, then they come out really messed up...

サインイン to comment.

サインイン してこの質問に回答します。

タグ


Translated by