Filter löschen
Filter löschen

How to add a string to a filename while saving plots

33 Ansichten (letzte 30 Tage)
Manal Shakeel
Manal Shakeel am 18 Apr. 2017
Kommentiert: Manal Shakeel am 25 Apr. 2017
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(; 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.

Akzeptierte Antwort

Stephen23 am 18 Apr. 2017
Use sprintf and fullfile. You will find a complete explanation here:
  3 Kommentare
Stephen23 am 18 Apr. 2017
Bearbeitet: Stephen23 am 18 Apr. 2017
@Manal Shakeel: like I said, use sprintf:
or perhaps:
and I am sure you can figure out how to put that into a loop.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Thorsten am 18 Apr. 2017
Bearbeitet: Thorsten am 18 Apr. 2017
name = 'test';
newname = [name, '_angle'];
newname = strcat(name, '_angle');
or, as Stephen suggested, using sprintf
newname = sprintf('%s_angle', name);


Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by