sprintf naming of figures

5 次查看(过去 30 天)
Hi I'm trying to run a loop and name files based on variables. The first two variables work correctly but the rest don't show. Observer , order, and orderS are strings and Rep is an integer (1 or 2). This is an example of what it outputs as is in my folder: S10_AHAPlot_ll_ave_.fig
%plots the data
plot(phase,AHA1,phase,AHA2,phase,AHA3,phase,AHA4,phase,AHA5,phase,AHA6,phase,AHA7,phase,AHA8,phase,AHA9,phase,AHA10,phase,AHA11,phase,AHA12,phase,AHA13,phase,AHA14,phase,AHA15,phase,AHA16);
legend('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16');
grid
%saves and names the data
filename = sprintf('S10_AHAPlot_%s_%s_%s_R%u.fig', order(k), orderS(y)),Observer,Rep;
saveas(gcf,filename);
  5 个评论
Stephen23
Stephen23 2020-5-7
编辑:Stephen23 2020-5-7
"Sure that's probably more efficient..."
It isn't just more efficient in terms of runtime, it is also more efficient in terms of your code writing and debugging time.
"I have no issues running the program other than making it name the files correctly."
Actually you do, although you might not realize it. For example, you are doing the computer's job for it.
Computers are really only good at one thing: doing simple tasks repeatedly in loops. Really everything your computer does boils down to that. So when you sit a copy-paste code or write out the names of lots of variables, you are actually just doing the menial task that your computer does better and much faster than any human can.
Having lots of numbered variables is a sign that you are doing something wrong. Using arrays is the correct solution.
Saar Peles
Saar Peles 2020-5-7
I'll edit the code if it ever needs to be update but since it's written theres not much point. I understand why you'd be frustrated with my answer, but I don't code for a living and this was a minor issue just with the name of the files saved. I didn't spend more than an hour trying to fix the issue, but I'm not super experiences, as you can tell.
The total program takes seconds to run and works fine albeit bad practice. I've just noticed the issue and it was an extra paranthesis. Regardless, thanks for your time.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2020-5-7
编辑:Stephen23 2020-5-7
The closing parenthesis is in the wrong place:
sprintf('S10_AHAPlot_%s_%s_%s_R%u.fig', order(k), orderS(y)), Observer, Rep; % wrong
sprintf('S10_AHAPlot_%s_%s_%s_R%u.fig', order(k), orderS(y), Observer, Rep); % right

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by