Help definitely needed! How do I load multiple txt files and change the varible names of each to include the date from file
1 次查看(过去 30 天)
显示 更早的评论
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!!
0 个评论
采纳的回答
Mischa Kim
2014-2-17
编辑:Mischa Kim
2014-2-17
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';]);
fclose(fid);
end
7 个评论
Jos (10584)
2014-2-19
It all depends on what your doing. If you only need to process a single file at a time, why not store the values into a variable that always has the same name.
filename = 'xxx.txt'
A = load(filename) % keeping a fixed name ...
Result = Myfunction(A) % ... makes further coding easier
AnotherResult = MyOtherFunction(A)
disp(['A result of processing file: ' filename])
disp(Result)
I see no point in switching variable names here, as you have to edit the code every time. In humble my view, variables should be seen as coding elements that have a fixed name , but variable contents .
Jos (10584)
2014-2-19
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!!
更多回答(1 个)
Jos (10584)
2014-2-17
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
fclose(fid);
end
Now the contents of MyData{x} or MyDataToo(k).data corresponds the file with the name MyFiles{x}
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!