How to activate loop in adress

1 次查看(过去 30 天)
Karl
Karl 2013-9-30
评论: Karl 2013-9-30
The loop in the adress in the code below does not work. The stored file gets the filename "fig(Vars2{iVars})" instead of the intented filename "figVariable names Vars". Any ideas on how to fix this?
Vars = {FordH};
Vars2 = {'Variable names Vars'};
for iVars = 1:length(Vars);
aVars = Vars{iVars};
fig = figure,title(Vars2{iVars});
hold on
plot(aVars(:,:));
end
hold off
print(fig,'-dpng','Q:\\XX\\Matlab\\YY\\Figures\\fig(Vars2{iVars}).png')

采纳的回答

Jan
Jan 2013-9-30
Yes, of course the file get the name you have specified. Anything between the single quotes is a string, an Matlab cannot guess, that you want the characters to be interpreted as name of a variable. This would be too magic.
The code contains several problems:
  1. The iVars loop is closed by end before the print command. In each iteration a new figure is created and only the last one is printed. But inside the print command, you want to use the loop counter iVars. The value of the loop counter is not well defined outside the loop, so if this is really the wanted behavior, prefer Vars2{end}.
  2. It seems like "Q:\\XX\\Matlab\YY" could be the Matlab root folder. If so, don't do this. Never write files into Matlab's root directory.
  3. If you want Vars{iVars} to be interpreted as variable:
print(fig,'-dpng', ['Q:\\XX\\Matlab\\YY\\Figures\\fig(', Vars2{iVars}), '.png'])
But "fig(Variable names Vars).png" is still a strange name.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by