Adding parameter to file name string when saving a figure using savefig or figsave

29 ビュー (過去 30 日間)
I am trying to save a figuer using figsave or savefig, with both string parameter and string. But nothing works.
CHANNEL=1;
dt = datestr(now,'yymmdd_HHMMSS');
filename = sprintf("C15_rhs_CalADC_ch%i_%s",CHANNEL,dt);
figsave(gcf,filename,'ac');
saveas(gcf,filename,'ac');
I need line 4 or line 5 to work to save the figuer as : C15_rhs_CalADC_ch6_220919_121449_ac

採用された回答

Star Strider
Star Strider 2022 年 9 月 19 日
移動済み: Rik 2022 年 9 月 20 日
I am not certain what you want.
Perhaps —
CHANNEL=1;
dt = datestr(now,'yymmdd_HHMMSS');
filename = sprintf("C15_rhs_CalADC_ch%i_%s_",CHANNEL,dt) + "ac"
filename = "C15_rhs_CalADC_ch1_220919_140304_ac"
.
  4 件のコメント
Bakr Abdelgaliel
Bakr Abdelgaliel 2022 年 9 月 20 日
移動済み: Rik 2022 年 9 月 20 日
Of course, please do it.
Star Strider
Star Strider 2022 年 9 月 20 日
@Bakr Abdelgaliel — As always, my pleasure!
@Rik — Thank you!

サインインしてコメントする。

その他の回答 (2 件)

Chunru
Chunru 2022 年 9 月 19 日
編集済み: Chunru 2022 年 9 月 20 日
CHANNEL=1;
dt = datestr(now,'yymmdd_HHMMSS');
% filename = sprintf("C15_rhs_CalADC_ch%i_%s_ac",CHANNEL,dt); % with ac
% plot(randn(6,1))
% %figsave(gcf,filename,'png'); % figsave is a customized function?
% saveas(gcf,filename,'png') % use file type matlab recognizes
filename = sprintf("C15_rhs_CalADC_ch%i_%s",CHANNEL,dt); % with ac
plot(randn(6,1))
%figsave(gcf,filename,'png'); % figsave is a customized function?
saveas(gcf,filename+"_ac",'png') % use file type matlab recognizes
ls % the file is saved
C15_rhs_CalADC_ch1_220920_070606_ac.png
  5 件のコメント
Rik
Rik 2022 年 9 月 19 日
Presumably 'ac', 'dc', etc are stored in a variable. Why don't you put the variable name in there?
SomeVariable='ac';
filename = sprintf("C15_rhs_CalADC_ch%i_%s_%s",CHANNEL,dt,SomeVariable);
Bakr Abdelgaliel
Bakr Abdelgaliel 2022 年 9 月 20 日
Thanks for your help

サインインしてコメントする。


Karim
Karim 2022 年 9 月 19 日
編集済み: Karim 2022 年 9 月 19 日
Do you mean savefig? See below fo a method to create the filename as you indicate and a demo to save and open the figure.
CHANNEL = 1;
dt = datestr(now,'yymmdd_HHMMSS');
% concatenate the different strings, use _ as delimiter
filename = join( ["C15_rhs_CalADC_ch",num2str(CHANNEL),dt,"ac"] , "_" )
filename = "C15_rhs_CalADC_ch_1_220919_140322_ac"
figure
plot(rand(20,1))
axis equal
grid on
% save the figure
savefig(gcf, filename)
% try to open the figure
openfig(filename)
ans =
Figure (2) with properties: Number: 2 Name: '' Color: [1 1 1] Position: [671 661 577 433] Units: 'pixels' Show all properties
  1 件のコメント
Bakr Abdelgaliel
Bakr Abdelgaliel 2022 年 9 月 19 日
I know that, but i don't like to change the parameter (filename) because i am going to use it several times with 'ac', 'dc', 'ADC', 'DAC', etc..

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePrinting and Saving についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by