Importing multiple text files into MATLAB

I am trying to import multiple text files into MATLAB and save it as a .mat file after it has been imported. It works when I do it one file at a time by using the following command, for example:
T1_1041 = importdata('20101011_114543_VIN9375_Trigger01.txt');
but when I try to save the file name into a variable and try to use the importdata function with that variable as the file name. It does not work, like below:
myfile = '20101011_114543_VIN9375_Trigger01.txt'
[files,path] = uigetfile('MultiSelect', 'on');
for n=1:length(files);
myfile = files(n);
files(n).data=importdata(myfile);
save files(n).data
end
It gives an error inside the importdata function.

1 件のコメント

Jan
Jan 2011 年 2 月 7 日
Please use the code formatting to improve the readability. If you get an error, it would be very helpful to post the error message.

サインインしてコメントする。

 採用された回答

Jan
Jan 2011 年 2 月 7 日

4 投票

Your variable "files" is a cell string, therefore you need curly braces. During the loop you overwrite the original "files" by a struct. The variable "myfile" is overwritten also - it looks really confused.
[filenames, pathname] = uigetfile('MultiSelect', 'on');
filenames = cellstr(filenames); % EDITED
for n = 1:length(filenames)
afile = fullfile(pathname, filenames{n}); % EDITED
data = importdata(afile);
% Remove the file extension file the name:
[dummy, afilename] = fileparts(filenames{n});
save(afilename, 'data');
end
If you want to save all data in one MAT file, explain this again with more details.

4 件のコメント

Harshit Coutinho
Harshit Coutinho 2011 年 2 月 7 日
ok i tried the code above that you suggested and it still does not work. The error that i get is
??? Error using ==> importdata at 114
Unable to open file.
Error in ==> trial at 12
fi =importdata(files{1});
Let me explain this again. When i run the file it opens a file browser window. I select the files that I want to open. It save those file names in files. I want to then use the stored file names with the importdata function by using the variable files{n} as the file name. But it does not work. But if i type the filename into the importdata function then it works.
Doug Eastman
Doug Eastman 2011 年 2 月 7 日
Can you tell us what files{1} is? Just add files{1} with no semicolon on line 12 before the call to importdata.
I would mention that you need to be a little careful because if you only select one file from the dialog it is not a cell array, the filename is stored directly as a string in files. However if this was the problem you were facing I would expect the following error message:
??? Cell contents reference from a non-cell array object.
So there must be something else going on.
Doug Eastman
Doug Eastman 2011 年 2 月 7 日
Another question that would cause this problem - do you change directories in the file selection gui?
Jan
Jan 2011 年 2 月 7 日
I've inserted two changes in the code. Please try it again.

サインインしてコメントする。

その他の回答 (1 件)

Doug Eastman
Doug Eastman 2011 年 2 月 7 日

2 投票

To be more robust to directory changes or single file selections, you might want to use something like the following:
[files,path] = uigetfile('MultiSelect', 'on');
if iscell(files)
for n = 1:length(files)
data = importdata(fullfile(path,files{n})); % fullfile including path
% save data
end
else
data = importdata(fullfile(path,files));
% save data
end

Community Treasure Hunt

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

Start Hunting!

Translated by