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

조회 수: 8(최근 30일)
I am trying to save a figuer using figsave or savefig, with both string parameter and string. But nothing works.
dt = datestr(now,'yymmdd_HHMMSS');
filename = sprintf("C15_rhs_CalADC_ch%i_%s",CHANNEL,dt);
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일
Moved: Rik 2022년 9월 20일
I am not certain what you want.
Perhaps —
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

댓글을 달려면 로그인하십시오.

추가 답변(2개)

Chunru 2022년 9월 19일
편집: Chunru 2022년 9월 20일
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
%figsave(gcf,filename,'png'); % figsave is a customized function?
saveas(gcf,filename+"_ac",'png') % use file type matlab recognizes
ls % the file is saved
  댓글 수: 5

댓글을 달려면 로그인하십시오.

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.
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"
axis equal
grid on
% save the figure
savefig(gcf, filename)
% try to open the figure
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..

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by