save in a folder

407 visualizaciones (últimos 30 días)
joseph Frank
joseph Frank el 3 de Mzo. de 2013
Editada: Image Analyst el 10 de Nov. de 2022
Hi,
I want to save in a folder mat files with a changing name.
for i=1:length(ID)
Filename=[num2cell(ID(i)) '.mat'];
save('C:\Users\Documents\MATLAB\TechnicalFinal\Filename','Close')
end
I am ending up with a file called Filename.mat instead of 1.mat,2.mat,and 3.mat where 1,2,3 are the ID number in the loop. Is there a way to fix that? Best

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 3 de Mzo. de 2013
Editada: Azzi Abdelmalek el 3 de Mzo. de 2013
use
for k=1:5
Filename=sprintf('%d.mat',k);
end
Also
save(['C:\Users\Documents\MATLAB\TechnicalFinal\' Filename],'close')
Because
filename='2.mat'
folder='C:\Users\filname'
Result
C:\Users\filname
and now
filename='2.mat'
folder=['C:\Users\' filename]
Result
C:\Users\2.mat

Más respuestas (1)

Image Analyst
Image Analyst el 3 de Mzo. de 2013
Editada: Image Analyst el 10 de Nov. de 2022
Follow the instructions in The FAQ. It covers changing mat filenames in a loop. It's also good to use fullfile() in addition to sprintf() to build the complete filename, as the second FAQ example shows.
folder = 'C:\Users\Documents\MATLAB\TechnicalFinal'; % Or wherever you want.
for k = 1 : length(ID)
% Create the base file name from the number contained in the "ID" variable.
baseFileName = sprintf('%3.3d.mat', ID(k)); % For example '037.mat'.
% Prepend the folder to get the full file name.
fullFileName = fullfile(folder, baseFileName);
% Save the variable (badly) named "Close" to this mat file.
save(fullFileName, 'Close');
end
I use 3.3d so that we will have 3 digits for the base file name and they will sort properly when the names are retrieved from dir() or when looking at the files in File Explorer. This will handle up to 999 files. If you have more than that increase the 3 to 4 or 5 or whatever is needed.

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by