sprintf naming of figures

3 views (last 30 days)
Saar Peles
Saar Peles on 6 May 2020
Edited: Stephen23 on 7 May 2020
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 Comments
Stephen23
Stephen23 on 7 May 2020
Edited: Stephen23 on 7 May 2020
"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 on 7 May 2020
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.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 7 May 2020
Edited: Stephen23 on 7 May 2020
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

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!