When using saveas command in a driver file to save figures, is there a way to automatically name the saved figure after the values of a parameter defined in the function file?

2 vues (au cours des 30 derniers jours)
Hello all,
I'm using fsolve to find the steady state solutions to an ordinary differential equation with three parameter values (a, r, and q). I have a function file that defines the ODE and the values of the parameters that I want to evaluate at . The driver file calls the function file with fsolve and loops 100 times to find steady state solutions at 100 random initial conditions, then plots all of the steady states found. (see full code below)
I'm running this code and looking at the plots for various parameter values. I want to save each plot and name the .fig file with the parameter values of a and r that were used in the function file.
This is the saveas line of code that I've been using in the driver file:
saveas(gcf,'/Users/Me/Matlab Figures/r=0.5 a = 0.0075.fig','fig')
Here's my problem - I can save the figures without a problem, but I have to manually change the parameter values of a and r in the saveas line for every iteration. This is becoming tedious because I'm running the code and saving the figures for a large array of parameter values.
Is there a command that I can use that will automatically input the current a and r values from the function file as the name of the .fig file that is being saved? I've been toying with it for quite some time and can't seem to get it right.
Here's the entirety of the code:
Function File: FixedPoints.m
function F = FixedPoints(u)
a=.0075;
r=.5;
q=10;
N=50;
[D x]=BookCheb(N);
D2=D*D;
D2=D2(2:N,2:N);
x=x(2:N);
F=(a*D2*u)+r*u.*(1-(u/q))-(u.^2./(1+u.^2));
end
Driver File: FSolveFixedPoints.m
N=50;
[D x] =BookCheb(N);
D2=D*D;
D2=D2(2:N,2:N);
x=x(2:N);
for i=1:100
u0=rand(N-1,1);
options=optimset('Display','iter'); % Option to display output
[u,fval] = fsolve(@FixedPoints,u0,options);
plot(x,u)
xlabel('x','FontSize',16)
ylabel('u(x)','FontSize',16)
title('r = 0.5 a = 0.0075 q = 10','FontSize',15,'FontWeight','bold')
hold all;
end
saveas(gcf,'/Users/Cay/Dropbox/M 480/Spruce Budworm/Matlab Figures/FSolve for Steady States/r=0.5/a = 0.0075.fig','fig')
All pointers and suggestions are greatly appreciated. Thanks in advance.

Réponse acceptée

Mahdi
Mahdi le 25 Mar 2013
Modifié(e) : Mahdi le 25 Mar 2013
I would suggest predefining a variable and using strcat to update the values.
Instead of what you had before, you can just put in a variable name for a string and update these values, here's how to do it:
SaveFileName=strcat('/Users/Me/Matlab Figures/r=', num2str(r), 'a=' num2str(a))
Then you can simplyuse saveas
saveas(gcf, SaveFileName, 'fig')
Just put that line in every loop. In the above num2str(r) basically change the variable r into a string (from a number) and updates it every time.
Hope this helps!

Plus de réponses (0)

Catégories

En savoir plus sur Printing and Saving dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by