MATLAB Answers

sprintf naming of figures

1 view (last 30 days)
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

Show 2 older comments
dpb
dpb on 7 May 2020
Then you don't have running code...and unless this research is over in the next week, the effort to recast is likely to repay itself many times over before you're done trying to modify this for the next steps.
That advice aside, we can't see what specifically could be wrong with the output string code without representative data
>> sprintf('S10_AHAPlot_%s_%s_%s_R%u.fig', 'ORDERSTRING', 'ORDERSSTRING','Observer',1)
ans =
'S10_AHAPlot_ORDERSTRING_ORDERSSTRING_Observer_R1.fig'
>>
works as expected if the inputs are what is expected by the format string.
Stephen Cobeldick
Stephen Cobeldick 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

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

  0 Comments

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!