Saving images with a name that change with the distance
5 views (last 30 days)
Show older comments
Hello !
I'm working on a diffraction simulation, and I plot figures of the diffraction pattern along the z axis with a vector called d that starts at 1cm, then 5cm, 10cm, etc.
I already did a command to give a title on figures that depends of d(i) with "num2str(d(i))", but I tried with the saveas command like this :
path = 'C:\this\is\not\a\real\path';
saveas(gca, fullfile(path,['double_fente_larg5ecart20_d',num2str(d(i)),'cm_l1mm']), 'jpeg');
But it does not work and give a .0.01cm_l1mm extension name to the saved files...
How could I solve my problem ?
Thanks for the help, and sorry for my english (i'm french)
0 Comments
Answers (1)
Deepak
on 8 Jan 2025
We can achieve correct file naming in a diffraction simulation by using "sprintf" to format d(i) with the desired precision, ensuring filenames reflect the intended measurement without extra decimals. By constructing the path with "fullfile" and saving with "saveas", we generate precise filenames that accurately correspond to the simulation values, avoiding formatting issues.
Below is the MATLAB code to achieve the same:
path = 'C:\this\is\not\a\real\path';
% Use sprintf to format the number with no decimal places or specify the desired precision
filename = sprintf('double_fente_larg5ecart20_d%.0fcm_l1mm', d(i)); % Adjust %.0f to the desired precision
% Use fullfile to construct the full path
fullFilePath = fullfile(path, filename);
% Save the figure
saveas(gca, fullFilePath, 'jpeg');
Please find attached the documentation of functions used for reference:
I hope this helps in resolving the issue.
1 Comment
Walter Roberson
on 8 Jan 2025
I would suggest
filename = sprintf('double_fente_larg5ecart20_d%02dcm_l1mm', round(d(i)*100));
maybe even %03d if the distances can reach 100cm
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!