creating a list of strings with different numbers and same letters

5 vues (au cours des 30 derniers jours)
void22
void22 le 20 Juin 2018
Modifié(e) : Stephen23 le 18 Avr 2021
i have a folder with files named as follows: f0K.csv f1K.csv f2K.csv and so on...
i want to create a for loop that will assign the data inside each file to a matrix using xlsread somthing like this :
if true
% code
for i = 0:100
MyFiles(i,:) = xlsread('f*i*K.csv','E1:E2500');
end
end
i know that there is an option to do so but haven't found it (i probably just don't know how to search! ) also , i know it's easy to do with dir('MyFolder') but for some mysterious reason its not working (i made sure its in the path ) so i'm trying to pass that obstacle in adifferent way
would appreciate your kind help !!!

Réponse acceptée

Stephen23
Stephen23 le 20 Juin 2018
Modifié(e) : Stephen23 le 18 Avr 2021
This is easy using sprintf:
D = 'MyFolder';
for kk = 0:100
fnm = fullfile(D,sprint('f%dK.csv',kk));
MyFiles(kk+1,:) = xlsread(fnm,'E1:E2500');
end
Or using dir and my FEX submission natsortfiles to get the correct numeric order:
D = 'MyFolder'
S = dir(fullfile(D,'f*K.csv'));
S = natsortfiles(S); % alphanumeric sort by filename
for k = 1:numel(S)
F = fullfile(D,S(kk).name;
MyFiles(k,:) = xlsread(F,'E1:E2500');
end
Don't forget to preallocate the output array before the loop:

Plus de réponses (0)

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by