Effacer les filtres
Effacer les filtres

How to append a string to a filename when saving a file?

60 vues (au cours des 30 derniers jours)
L'O.G.
L'O.G. le 30 Mar 2022
Commenté : Les Beckham le 30 Mar 2022
How do I append a string to the filename and save the file in the case of saveas and save? The following isn't quite right, so I'd appreciate some insight.
[~,f,ext] = fileparts(files(id).name); % extract file name without original extension
% do some calculations and generate a plot based on the original file
str_saveas = sprintf(str_saveas,f);
saveas(fig,'str_saveas','png')
And similarly, for the save function.
  1 commentaire
Geoff Hayes
Geoff Hayes le 30 Mar 2022
@O.G. what is
str_saveas
? If you just want to append two string variables together, you could use the square brackets as
str_saveas = [str_saveas f];
sprintf would be used more like
str_saveas = sprintf('%s%s', str_saveas, f);

Connectez-vous pour commenter.

Réponse acceptée

Les Beckham
Les Beckham le 30 Mar 2022
This should get you closer to what you want to do
[~,f,ext] = fileparts(files(id).name); % extract file name without original extension
% do some calculations and generate a plot based on the original file
str_append = 'text_that_you_want_to_add';
str_saveas = sprintf('%s_%s', f, str_append); % add the appended string to the original filename
saveas(fig, str_saveas, 'png') % note no quotes on the filename since it is a variable
  2 commentaires
L'O.G.
L'O.G. le 30 Mar 2022
Thanks!
Les Beckham
Les Beckham le 30 Mar 2022
You are welcome.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Analysis dans Help Center et File Exchange

Tags

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by