Saving images with a name that change with the distance
5 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Deepak
2025 年 1 月 8 日
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 件のコメント
Walter Roberson
2025 年 1 月 8 日
I would suggest
filename = sprintf('double_fente_larg5ecart20_d%02dcm_l1mm', round(d(i)*100));
maybe even %03d if the distances can reach 100cm
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!