Doug’s MATLAB Video Tutorials

July 2nd, 2009

Advanced: loading files using their names for variable names

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:

Processing files in a directory with MATLAB

Dynamic field name usage in MATLAB

6 Responses to “Advanced: loading files using their names for variable names”

  1. jason replied on :

    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.

  2. jason replied on :

    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:

    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
    

    Thanks (again) to Doug,
    Jason.

  3. dhull replied on :

    Thanks for the follow up. Hopefully at least one other MATLAB user will find this useful!

    -Doug

  4. Carlos Adrian Vargas Aguilera replied on :

    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);
    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

    Carlos

  5. jhuang replied on :

    exactly what i needed :)

    thanks doug

  6. Ashok kumar replied on :

    Thanks for the video tutorial. It made my life easy.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

These postings are the author's and don't necessarily represent the opinions of The MathWorks.