Why Saveas appaer a error sometimes

23 views (last 30 days)
Bastien
Bastien on 9 May 2014
Answered: Any Moose on 11 Sep 2017
Hello I try to use "Saveas" to save figure. However it's working some time and sometime a error appear :
Error using savefig (line 41) Argument must be a .fig file
Error in saveasfig (line 6) savefig(h, name);
Error in saveas (line 140) feval( ['saveas' format], h, name )
Error in Plot_Integration_Time (line 215) saveas(H,[path_res 'Delta Tension (V) At 5V and ' num2str(Frequency) 'HZ'],'fig');
I don't unterstand why it's not working. Because H is a figure.
a part of my code :
H=figure;
plot(Wait_time(Delta_Ind_5V_1s),Delta_Tension(Delta_Ind_5V_1s),'-xc',...
Wait_time(Delta_Ind_5V_2s),Delta_Tension(Delta_Ind_5V_2s),'-xr',...
Wait_time(Delta_Ind_5V_3s),Delta_Tension(Delta_Ind_5V_3s),'-xg',...
Wait_time(Delta_Ind_5V_4s),Delta_Tension(Delta_Ind_5V_4s),'-xb',...
Wait_time(Delta_Ind_5V_5s),Delta_Tension(Delta_Ind_5V_5s),'-xk',...
Wait_time(Delta_Ind_5V_1s),Warning(Delta_Ind_5V_1s),'xc',...
Wait_time(Delta_Ind_5V_2s),(1.5*Warning(Delta_Ind_5V_2s)),'xr',...
Wait_time(Delta_Ind_5V_3s),(2*Warning(Delta_Ind_5V_3s)),'xg',...
Wait_time(Delta_Ind_5V_4s),(2.5*Warning(Delta_Ind_5V_4s)),'xb',...
Wait_time(Delta_Ind_5V_5s),(3*Warning(Delta_Ind_5V_5s)),'xk');
xlabel('N° of Time Integration (-)');
ylabel('Delta Tension (V)');
title(['At 5V and ' num2str(Frequency)]);
legend('1s','2s','3s','4s','5s','Warning 1s','Warning 2s','Warning 3s','Warning 4s',...
'Warning 5s','location','Best');
grid on;
saveas(H,[path_res 'Delta Tension (V) At 5V and ' num2str(Frequency) 'HZ'],'fig');
If somebody could help me ;)
Thank you
Bastien

Answers (3)

Bruno Pop-Stefanov
Bruno Pop-Stefanov on 9 May 2014
Can you try with something more simple, like
savesas(gcf, 'myfigure.fig')
?
Does the figure save correctly if you do it manually in the figure window (File > Save...)?
  1 Comment
Bastien
Bastien on 12 May 2014
Yes savesas(gcf, 'myfigure.fig') save correctly and the manually way to
actually my code works sometime but for some mesure it's doesn't work.

Sign in to comment.


dpb
dpb on 9 May 2014
saveas(H,[path_res 'Delta Tension (V) At 5V and ' num2str(Frequency) 'HZ'],'fig');
Your filename contains embedded blanks. If you insist on using such, you must enclose them in the double-quotes (") to keep it as a single string throughout the intermediate code.
fn=['"' path_res 'Delta Tension (V) At 5V and ' num2str(Frequency) 'HZ"'];
saveas(H,fn,'fig');
perhaps altho you don't show what path_res definition is. Perhaps also fullfile might be of use here.
  4 Comments
dpb
dpb on 12 May 2014
Edited: dpb on 12 May 2014
A) Return and check error status
B) Step thru w/ debugger and ensure strings are valid.
C) Which OS? '\' is valid only for Windows I think???
C) You are intending to build multi-level directory structure here?
I'm guessing it's failing 'cuz of either an already existing path left over from a previous failed test or run or mis-specified path somewhere. Always check returns for success on such stuff so you know where it first went south. The problem may have come way back when so backtracking is much harder if so...
ADDENDUM
You're making awfully complicated file names--that leaves a lot of room for accidentally creating one that isn't valid somehow if a variable isn't what you expect. Particularly I'm wondering about your num2str portion perhaps being a floating point representation? Not sure what that would do to a file name otomh, but it wouldn't be pretty at best.
I'd suggest at least while debugging to build the file name as a separate variable and echo it to the command window so you see what it is and which ones succeed and fail when it happens. That should shorten the debug time significantly.
I'd still also suggest simplifying your naming scheme significantly and getting rid of the spaces--my experience has been they're fraught w/ difficulties to ensure always handled programmatically. It's an abomination M$ should've never introduced.
Image Analyst
Image Analyst on 12 May 2014
Good points by dpb. I also recommend use of fullfile() to build full pathnames, and sprintf() to create the base file name. I never use that num2str() method and always use sprintf() instead.
Also, using forward slash for all operating systems works fine. Windows has no problem with forward slashes - it doesn't have to be a backslash, which can sometimes be a problem in sprintf() where you have to use \\ if you want a single \.

Sign in to comment.


Any Moose
Any Moose on 11 Sep 2017
Make sure there are no '.' in your filenames. Otherwise Matlab may try to save it as a filetype of .5Hz.fig; which won't work!
fn = replace(fn,".","-")

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!