In this short video we will show how to bring text files into MATLAB and use the original file name as the variable name. Actually, the file name will be used as a field name in a structure.
Some people would use the EVAL statement or ASSIGNIN to do this, however doing so would disallow the JIT accelerator from speeding the code up as much as it could. The skills taught in this video are related to the ones in these videos:
I was just thinking about this last week. Is there a comparable solution, using load instead of eval, that would also keep the Matlab compiler happy with scripts (.m files)?
For example, if I had a series of parameters as a script (a .m file), this won’t work with the compiler:
So, thanks to Doug’s help, it turns out that the EVAL in was not in fact the real problem. Rather, it was that the associated m-file was not compiled with the main function. In this case, I have hundreds of these associated m-files based on site × date combinations and didn’t want to have to recompile each time I had a new dataset with a new site × date combination. The solution is then to not use the script-based m-file (that required being compiled) and instead use a csv file.
See here for the details on compiling with associted scripts:
A basic example of the old script-based m-file is:
Latitude = 49.3824;
Longitude = -80.4862;
Altitude = 300;
As a csv file, this is:
Latitude,49.3824
Longitude,-80.4862
Altitude,300
To make those variables available and to replace the
eval([filename '_model_parameters']);
command, I have this:
% Get data from csv files
fid = fopen(filename '_model_parameters.csv');
inputs = textscan(fid,'%s%f','delimiter',',');
fclose(fid);
% Separate variables from values
vars = genvarname(inputs{1,1});
vals = inputs{1,2};
% Assign values to variables
for i = 1:size(vars,1)
eval([cell2mat(vars(i)) '= vals(i);']);
end
Now I prefer to use another way. First I search for the files names and create a list with the LS command (in windows) which allows me to use the wildcard ‘*’ and then I just pick the file I need. So, your code will look like this:
list = ls('data*.txt'); % Files names on rows (WIN OS)
for k = 1:size(list,1)
fileName = deblank(list(k,:)); % Name of k-file
dataStruc.(fileName) = load(fileName);
end
Of course, there is an extra line, and one should be aware that all ‘data*.txt’ files will be read! Other safer way is to save the filenames on a file and read instead of use LS. Well, just a thought
Hi
quick question
can I pipe the names of files in a directory in to a variable array
meaning if I have bunch of text files *.asf
how can I (if possible) assign results of ls *.asf
into a variable that I can use (like a vector member by member)
thanks
I have same problem but my files are in “.CSV” format (Microsoft Office Excel Comma Separated Values File) and I use “csvread“to load one of my files, so I used:
adding onto the discussion, I wanted to do the same thing as Jason, but instead of just doubles, I had arrays of numbers and strings that I wanted to assign to variables. A slight modification of Jason’s code is below which allows one to read in variables from file assign the variables in the matlab workspace.
it requires a semicolon separating all variable definitions as well as commas between array elements. Besides those two qualifications, it follows matlab syntax. I hope this is useful to other matlab users!
I was just thinking about this last week. Is there a comparable solution, using load instead of eval, that would also keep the Matlab compiler happy with scripts (.m files)?
For example, if I had a series of parameters as a script (a .m file), this won’t work with the compiler:
eval([filename '_model_parameters']);
Is there an equivalent with load that would work?
Thanks for the videos.
Jason.
So, thanks to Doug’s help, it turns out that the EVAL in was not in fact the real problem. Rather, it was that the associated m-file was not compiled with the main function. In this case, I have hundreds of these associated m-files based on site × date combinations and didn’t want to have to recompile each time I had a new dataset with a new site × date combination. The solution is then to not use the script-based m-file (that required being compiled) and instead use a csv file.
See here for the details on compiling with associted scripts:
http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/f13-1003481.html#f13-1003495
A basic example of the old script-based m-file is:
As a csv file, this is:
Latitude,49.3824
Longitude,-80.4862
Altitude,300
To make those variables available and to replace the
command, I have this:
% Get data from csv files fid = fopen(filename '_model_parameters.csv'); inputs = textscan(fid,'%s%f','delimiter',','); fclose(fid); % Separate variables from values vars = genvarname(inputs{1,1}); vals = inputs{1,2}; % Assign values to variables for i = 1:size(vars,1) eval([cell2mat(vars(i)) '= vals(i);']); endThanks (again) to Doug,
Jason.
Thanks for the follow up. Hopefully at least one other MATLAB user will find this useful!
-Doug
Nice post Doug.
I used to work with my ascii files as you explain. I even write a code called INT2STRZ
http://www.mathworks.com/matlabcentral/fileexchange/12973
to work around with files named data001.txt, data002.txt, etc.
Now I prefer to use another way. First I search for the files names and create a list with the LS command (in windows) which allows me to use the wildcard ‘*’ and then I just pick the file I need. So, your code will look like this:
list = ls('data*.txt'); % Files names on rows (WIN OS) for k = 1:size(list,1) fileName = deblank(list(k,:)); % Name of k-file dataStruc.(fileName) = load(fileName); endOf course, there is an extra line, and one should be aware that all ‘data*.txt’ files will be read! Other safer way is to save the filenames on a file and read instead of use LS. Well, just a thought
Carlos
Hi
quick question
can I pipe the names of files in a directory in to a variable array
meaning if I have bunch of text files *.asf
how can I (if possible) assign results of ls *.asf
into a variable that I can use (like a vector member by member)
thanks
@Mesut,
The Dir command should help.
Doug
Thanks!!
spent hours trying to figure this one out and it turns out to be beautifully simple.
One very grateful newbie
I have same problem but my files are in “.CSV” format (Microsoft Office Excel Comma Separated Values File) and I use “csvread“to load one of my files, so I used:
But didn’t work.
I have also some “.txt” files that have some header line and I use “importdata“to load one of my files, so I used:
But this didn’t work, too.
@Ramin,
Please make sure you are constructing the name of the file correctly. For testing, just write out the file name. does it import then?
Doug
Hi,
adding onto the discussion, I wanted to do the same thing as Jason, but instead of just doubles, I had arrays of numbers and strings that I wanted to assign to variables. A slight modification of Jason’s code is below which allows one to read in variables from file assign the variables in the matlab workspace.
my input looks something like this:
mtraj{1}=’src/ww1.assn.traj’;
mtraj{2}=’src/ww2.assn.traj’;
mtraj_to_use=[1, 2];
lag_i=[ 15 ];
lag=[1, 2, 4, 8, 10, 16, 25, 50, 100, 150, 200, 250 ,300, 400, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 8000, 10000];
low_mem_map_matrix=0;
occ_threshold=0;
symm=1;
macrostate_defs=[2, 4, 6, 8, 10, 20, 40, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 600, 700, 800];
it requires a semicolon separating all variable definitions as well as commas between array elements. Besides those two qualifications, it follows matlab syntax. I hope this is useful to other matlab users!