Why Saveas appaer a error sometimes

16 次查看(过去 30 天)
Bastien
Bastien 2014-5-9
回答: Any Moose 2017-9-11
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

回答(3 个)

Bruno Pop-Stefanov
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 个评论
Bastien
Bastien 2014-5-12
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.

请先登录,再进行评论。


dpb
dpb 2014-5-9
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 个评论
dpb
dpb 2014-5-12
编辑:dpb 2014-5-12
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 2014-5-12
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 \.

请先登录,再进行评论。


Any Moose
Any Moose 2017-9-11
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,".","-")

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by