How can I load my data from a variable filename

Hello, How can I load 80 files(.mat) from folder each has a number ex: file_1, file_2, file_3 ...file_80. What I did the following:
for x= 1:80
load ('file_x.mat')
But it's not working: gave me the following error: Error using load Unable to read file 'file_x.mat': no such file or directory.

 Réponse acceptée

You need to do this by converting the numerical value of "x" to a string and concatenating that with the rest of the file name string. For example:
for x= 1:80
load (['file_' num2str(x) '.mat'])
end
- Sebastian

5 commentaires

Stephen23
Stephen23 le 29 Juil 2015
Modifié(e) : Stephen23 le 29 Juil 2015
Note that it is much better to assign the data into an output variable, like this:
for k = 80:-1:1
S(k) = load(sprintf('file_%d.mat',k));
end
This avoids the need to dynamically name and access those variables.
Good call. Thanks for the answer upgrade!
I have a question because I have the same problem but the files are named file_000000 until file_000788. How would I do that? Thank you!
Are those the only files with that pattern of name in your directory?
D = dir('file_000*');
for k = 1:numel(D)
fprintf('Processing file %s.\n', D(k).name)
end
I have the question also because I have the same problem to load the data. I have 248 files with different name which is 94 files of ADmaskMean_AD_1 until ADmaskMean_AD_94, and 154 files of ADmaskMean_NORMAL_1 until ADmaskMean_NORMAL_154. How can I do that? Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by