Input String Variable to SaveAs Command
21 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Matthew
il 25 Set 2017
Modificato: Stephen23
il 25 Set 2017
I'm trying to write the SaveAs command to save a plot. Upstream of the command, I'm trying to create a string parameter that's comprised of a variable from the calculations. Below is the snippet of code that I'm working with. I'd like to capture the value of the "d" parameter in the SaveAs filename. I figured I might be able to do this by first writing a string variable having the "d" parameter within. Then I try to reference that string variable in the SaveAs command. It doesn't seem to work though. Is there another way to do this?
d=100E-6;
str_saveas="HW_2_%d_micron_particle";
z=d*10^6;
str_saveas=sprintf(str_saveas,z)
saveas(figure(1),'str_saveas','fig');
Thanks in advance, M Ridzon
1 Commento
Stephen23
il 25 Set 2017
Modificato: Stephen23
il 25 Set 2017
This is a sign of some bad code design:
str_saveas="HW_2_%d_micron_particle
z=d*10^6;
str_saveas=sprintf(str_saveas,z)
because you are putting meta-data into variable names. This is a very bad practice, and will only make your code slow, buggy, and complicated. This is exactly the problem you are experiencing now, although you probably don't realize it yet.
Read this to know why putting meta-data into variable names is a bad idea:
Some beginners think that putting meta-data into variable names is cunning and neat. It isn't, and it just causes them to write complex, buggy, slow code.
You should learn to use indexing: simple, efficient, neat, easy to write and debug. If you had used indexing then your example could be reduced down to two or three simpler lines of code.
Risposta accettata
Walter Roberson
il 25 Set 2017
Modificato: Walter Roberson
il 25 Set 2017
d = 100E-6;
str_saveas = 'HW_2_%d_micron_particle.fig';
z = d*10^6;
str_saveas = sprintf(str_saveas, round(z));
saveas(figure(1), str_saveas, 'fig');
saveas(figure(1), str_saveas, 'fig');
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Printing and Saving in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!