Clear Filters
Clear Filters

Importing text files into variables with the same name

9 views (last 30 days)
Hi all,
I am trying to load a series of txt files into matlab from a single directory. They all have two columns of numbers with the head "s" and "ACD 0", which looks somewhat like this:
"s" "ADC 0"
0.00000000 -0.031433
0.00020000 -0.025940
0.00040000 -0.030212 except that the "s" and "ACD0" are headers.
My question now is how I can load multiple files with that format into Matlab with their file names as variable names
So far I got:
files = dir('*.txt'); %loads all txt files
Names={};
for i=1:length(files);
Names{i} = files(i).name; %creates a cell with names
end
Names = Names';
Names_char= char(Names) %turns the names into char so I can use them as input
for i= 1: length(files);
eval ([dlmread(Names_char(i,:),'\t', 1)]);
end
however i always receieve ??? Undefined function or method 'eval' for input arguments of type 'double'
Alternatively I tried
files = dir('*.txt');
Names={};
for i=1:length(files);
Names{i} = files(i).name;
end
Names = Names';
Names_char= char(Names)
for i= 1: length(files);
eval (['M' dlmread(Names_char(i,:),'\t', 1)]);
end
However I get ??? Error using ==> horzcat CAT arguments dimensions are not consistent.
Do you have any ideas/suggestions how I could turn my text files into variables with the same name as the files?
Thanks
Ben
  2 Comments
Cedric
Cedric on 17 Oct 2013
Edited: Cedric on 17 Oct 2013
Please, format your post using [{}Code] in the editor when relevant, so the code is readable.
Also, note that building variable names dynamically is almost never a good idea. It is generally better to save data in a cell array and file names (in your case) in another cell array, with matching IDs. Another option is to use a mapping or a Java hash table.
We can discuss your code and the options that I mentioned once you have formatted your post.
Ben
Ben on 18 Oct 2013
Sorry, I didn't realise that I forgot to format my code.
Also the headers are not really relevant to my code, so they could be removed as I have done with
eval ([dlmread(Names_char(i,:),'\t', 1)]);

Sign in to comment.

Accepted Answer

Cedric
Cedric on 18 Oct 2013
Your attempts are not bad; we usually avoid using EVAL when possible and we don't generate variable names dynamically (I've seen just a few cases over my years with MATLAB where generating dynamically variable names was appropriate).
Here is one approach for reading your data files:
files = dir( '*.txt' ) ;
nFiles = length( files ) ;
names = cell( nFiles, 1 ) ;
data = cell( nFiles, 1 ) ;
for dId = 1 : nFiles
names{dId} = files(dId).name ;
fId = fopen( files(dId).name, 'r' ) ;
cols = textscan( fId, '%f %f', 'headerlines', 1 ) ;
data{dId} = [cols{:}] ;
fclose( fId ) ;
end
You will notice that file names and data are stored in cell arrays, with matching IDs between each name and corresponding data, i.e. data{3} corresponds to names{3}. The line
cols = textscan( fId, '%f %f', 'headerlines', 1 ) ;
creates a cell array of columns. I thought that you would prefer to have a matrix of data, so I implemented a concatenation with [cols{:}], which is what is stored in cell array data. Most often, we want to iterate through data sets and we need to display sometimes a file name that correspond to a given data set. In such case, having the two cell arrays is well adapted, e.g., for display all data sets with the file name as a header, you would do something like
for dId = 1 : length( data )
fprintf( '\nFile: %s\n', names{dId} ) ;
disp( data{dId} ) ;
end
Here you see that the data or file index is numeric. If, for some reason, you really needed to index a data set using a file name, you could use a Java hash table or a MATLAB mapping, or build a mechanism by yourself, e.g.
dId = strcmp( names, 'data2.txt' ) ; % Look for data corresponding to file 'data2.txt'.
if any( dId )
selection = data{dId} ;
else
error( 'No data found for given file name.' ) ;
end

More Answers (1)

dpb
dpb on 18 Oct 2013
As Cedric has already hinted, you really, Really, REALLY don't want to do this...see
for alternatives.
There are questions regularly on how to get past other problems created by having done just what you're about to do if don't use another method. Just within the last few days we've had a long discussion with another poster who dug himself a hole he can now get out of only w/ great difficulty.

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!