Help definitely needed! How do I load multiple txt files and change the varible names of each to include the date from file

2 views (last 30 days)
Hey everyone,
My coding skills have clearly dies on me with this one. I have a few txt files in directory that I am using textscan to load (massive files so I want to reduce memory). I would like a loop that goes through the list, loads in data into the workspace and gives each dataset a variable name that includes the date from the file name.
My file names are like this one midas_wind_198001-198012.txt from 1979-2013.
Here is the code I have thus far for loading but I am stuck on taking it further to renaming the variables. I do not want to use EVAL for efficiency reasons and I would like each dataset to be its own variable
fid = fopen('midas_wind_197701-197712.txt', 'r'); C = textscan(fid,'%s %s %f %f %s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %s %f', 'Delimiter',','); fclose(fid);
Thanks a lot in advance!!

Accepted Answer

Mischa Kim
Mischa Kim on 17 Feb 2014
Edited: Mischa Kim on 17 Feb 2014
Masao, assuming that the only the year changes in each filename, use
for ii = 1973:2013
f_name = strcat('midas_wind_',num2str(ii),'01-',num2str(ii),'12.txt');
fid = fopen(f_name, 'r');
scan_val = textscan(fid,'%s ... % !command cut off for readability!
v_name = genvarname(strcat('C',num2str(ii)));
eval([v_name ' = scan_val';]);
Jos (10584)
Jos (10584) on 19 Feb 2014
In addition, if you have two related variables, it is convenient to give them the same name but arrange them in an array:
Year(1) = 1990
Year(2) = 1996
Year(3) = 2001
Imagine using this design
Year1990 = 1990
Year1996 = 1996
Year2001 = 2001
Now see what happens if you need to change the third year
Year(3) = 1997 % easy to understand
Year2001 = 1997 % awkward!!

Sign in to comment.

More Answers (1)

Jos (10584)
Jos (10584) on 17 Feb 2014
You can use cell or structs.
MyFiles = {'midas_wind_197701-197712.txt','whatever.txt','another name.txt'}
for k=1:numel(MyFiles) % loop over all the files
fid = fopen(MyFiles{k}, 'r');
tmp = textscan(fid,'%s %s %f %f %s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %s %f', 'Delimiter',',');
% store
MyData{k} = tmp ; % option 1
MyDataToo(k).data = tmp ; % option 2
MyDataToo(k).filename = MyFiles{k} ; % for convenience you can store the
% filename in a separate field
Now the contents of MyData{x} or MyDataToo(k).data corresponds the file with the name MyFiles{x}
  1 Comment
mashtine on 24 Feb 2014
Hey Jos,
I have used your code to create a cell array with the data and struct array. Unfortunately, both take up a large space when being saved (over 10 GB) whereas with Mischa method, my .mat file is only about 500 MB. Any reason why saving the variables separately causes this?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by