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
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
exactly what i needed :)
thanks doug
Thanks for the video tutorial. It made my life easy.