reading multiple files using importdata

39 vues (au cours des 30 derniers jours)
HolgSpre
HolgSpre le 3 Oct 2016
Commenté : HolgSpre le 4 Oct 2016
Hi! Wanna import data from various .txt-files. And I am wondering why the code, I'm using, is not working. Probably I did a mistake in the format of entering the file name and directory. Anybody know what to do?
if true
pth = 'C:\Users\Marcus\Documents\MATLAB\data\'; % directory of the folder, where data files are saved
liste = dir(strcat(pth,'*.txt')); % creates a list of all files in the folder directory pth
files = {liste.name}; % creates a cell array with the name of the data file
for k = 1:numel(files);
fullname = strcat(pth,files) ;
data = importdata(fullname);
end

Réponses (1)

michael
michael le 3 Oct 2016
1. you have to do the dir command before the strcat
2. you can't do the stract on the whole list, you have to do it one by one
i.e
liste = dir('*.txt');
for i=1:length(liste)
files=strcat(pth,'\',liste.name(i))
end
  2 commentaires
HolgSpre
HolgSpre le 4 Oct 2016
Thank's for the help so far! I'm sorry that I did not specify in my question that the matlab-file will not be saved in the same folder as the data files. That's why I used
liste = dir(strcat(pth,'*.txt'));
unfortunately
liste = dir('*.txt');
is not working in that case...Any other ideas?
HolgSpre
HolgSpre le 4 Oct 2016
Finally it worked with:
pth = 'C:\Users\Marcus\Documents\MyDataFolder\';
liste = dir(strcat(pth,'*.txt'));
files = {liste.name};
for k = 1:numel(files);
file{k} = strcat(pth,files{k});
data{k} = importdata(file{k},' ',6);
end
Thanks for your help!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Large Files and Big Data dans Help Center 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