Stuart’s MATLAB Videos

Watch and Learn

Advanced: loading files using their names for variable names 22

Posted by Doug Hull,

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

22 CommentsOldest to Newest

jason replied on : 1 of 22
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.
dhull replied on : 2 of 22
Thanks for the follow up. Hopefully at least one other MATLAB user will find this useful! -Doug
jason replied on : 3 of 22
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. 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',',');

% 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);']);
Thanks (again) to Doug, Jason.
Carlos Adrian Vargas Aguilera replied on : 4 of 22
Nice post Doug. I used to work with my ascii files as you explain. I even write a code called INT2STRZ 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);
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
Mesut replied on : 5 of 22
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
Aaron replied on : 7 of 22
Thanks!! spent hours trying to figure this one out and it turns out to be beautifully simple. One very grateful newbie
Ramin replied on : 8 of 22
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:
fileName= [ 'data' num2str(i)];
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:
fileName= [ 'data' num2str(i)];
datastruct.(fileName)=importdata ([fileName '.txt'], ',', 18);
But this didn’t work, too.
dhull replied on : 9 of 22
@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
liz replied on : 10 of 22
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!
fid = fopen(input_file);                                                       
inputs = fscanf(fid,'%s');                                                      
limits = strfind(inputs,';');                                                   
ndx = 1;                                                                        
for(i = 1:length(limits))                                                       
  t = inputs(ndx:limits(i));                                                    
  t = strrep( t, ',' , ' ');                                                    
  ndx = limits(i) + 1;                                                          
Ali replied on : 11 of 22
Hi, I am using similar script for loading huge number of data sets. The problem I have is that the program does not run if i's are zero or negative. My original filenames are something like A-1, A0, A1,..., and so if I want to use this script I should change the names (before loading) to A1, A2, A3,... Is there a way to get around this? Thanks Ali
abhinav replied on : 13 of 22
Hi I am new to matlab. I read an excel file using xlsread. The file contains list of variables and values. I want to generate the workspace variables with the same name as the names in excel sheet dynamically and use the values to generate matrix for those variables. Can you tell me how I can achieve this? Thanks
doug replied on : 14 of 22
@Abhinav, Double click on the XLS file in the MATLAB current directory browser. The import there should be easy to use. Doug
Amardeep replied on : 15 of 22
Hey Doug, I found your tutorial on curve fitting very useful. I was wondering if there is a way to combine multiple CSV files into 1. Basically I have 100s of CSV files with 2 columns each. The first column remains same (X-axis) and I want to import 2nd column vectors of all the files into 1 file to make it easier to fit a curve. I will really appreciate your help. Thanks, Amardeep
Doug replied on : 16 of 22
Amardeep, Once you get each file read in, it should be a column. You should be able to loop through all the files as shown here: Once you have all these columns, you can just keep concatenating them to each other to form a matrix. Doug
Farley Postgate replied on : 17 of 22
Just like to thank Jason I believe for the above comment. This code allowed me to easily make an input file and the code creates the variables. % 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, Farley
Deepak replied on : 18 of 22
hi Dough, Thanx for this post. I have a bit different problem. 1- I have a zip file which contains numerous text files I have extracted and kept in a directory. 2- These text files have been named as per per some sequence. 3- I need to import all these files into matlab 4- So I created code to generate the file names 5- But the problem now is i cannot load the data using the importdata() as the function treats the variable as file name. Can u sujjest this can be done. Sample code: prompt = 'Enter the date in ddmmyyy format?'; ip= input(prompt); st4= num2str(ip); st2= '_'; st1='E'; prt_cds=importdata('port_codes.xlsx'); no_ports= size(prt_cds.Sheet1,1) for k= 1:no_ports st3=prt_cds.Sheet1(k) filename= strcat(st1,st2,st3,st2,st4) f=importdata('filename.txt') end
Linh Nguyen replied on : 19 of 22
After load datastructure I want to create one matrix include all filles in datatructure but i dont know how i need to do? Please tell me command to do it. Thanks
JohnDeuf replied on : 21 of 22
I have several .txt files of 2 columns, that I'd like to import and use the second column as a variables for subsequent operations. My text files are name as : profileA1.txt, profileA2.txt, etc... and my variables in the subsequent code are A1, A2, A3, etc... The code works but currently I have to open manually each txt.file with the Import data wizard, change the name of the second column and click on the import the selection. I tried to write a code (see below) to automatize these steps but it doesn't work? Anyone has any idea to fix this code? Thanks for k = 1:5 myfilename = sprintf('profileA%d.txt', k); mydata = importdata(myfilename); Aloop = ['A' num2str(k)]; A{k} = load(,2), k); end
Stuart McGarrity replied on : 22 of 22
John, After you call importdata, Isn't your data in the 'mydata' variable? What are you trying to do with the 'Aloop' variable?