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

60 次查看(过去 30 天)
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 个评论
Geoff Hayes
Geoff Hayes 2022-3-30
@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);

请先登录,再进行评论。

采纳的回答

Les Beckham
Les Beckham 2022-3-30
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by