How to add a string to a filename while saving plots

조회 수: 44 (최근 30일)
Manal Shakeel
Manal Shakeel 2017년 4월 18일
댓글: Manal Shakeel 2017년 4월 25일
I have a code that runs a for loop through a folder, reads the files, plots certain variables for each file and saves the plot as a png with the filename.
[pathstr,name,ext] = fileparts(file.name); print('-dpng',name,'d');
e.g: For a file called test the plot is saved as test.png
Now I want to plot multiple graphs for the same file and hence save the file as test_angle.png for example.
Any idea how I can do that? In python I can do so using '+'. I tried using strcat and append but I think I am doing something wrong.

채택된 답변

Stephen23
Stephen23 2017년 4월 18일
Use sprintf and fullfile. You will find a complete explanation here:
  댓글 수: 3
Stephen23
Stephen23 2017년 4월 18일
편집: Stephen23 2017년 4월 18일
@Manal Shakeel: like I said, use sprintf:
sprintf('%s_angle%s',name,ext)
or perhaps:
sprintf('%s_%s_angle%s',name,'a',ext)
and I am sure you can figure out how to put that into a loop.
Manal Shakeel
Manal Shakeel 2017년 4월 25일
Thanks!

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

추가 답변 (1개)

Thorsten
Thorsten 2017년 4월 18일
편집: Thorsten 2017년 4월 18일
name = 'test';
Either
newname = [name, '_angle'];
or
newname = strcat(name, '_angle');
or, as Stephen suggested, using sprintf
newname = sprintf('%s_angle', name);

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by