Getting variables for different files
Show older comments
I am trying to load multiple files and extract respective data from them. I have managed to successfully load the multiple files, but I am having trouble assigning each unique variable to them. I have uploaded 2 sample files, contained within a folder.
This means that I manage to loop through for the different areas, but each time the loop processes, the variables A,B,C changes.
I want it such that A1,B1,C1 is for the first area, A2,B2,C2 is for the second area and so on...how do I go about doing this? I tried A(k),B(k)C(k) but it did not work.
inFile = 'filepath\weather\';
location={'savannah-ga_temp','newyork-ny_temp','acadia-me_temp','boston-ma_temp','charleston-sc_temp','kingston-ri_temp'};
for k=1:length(location)
loc = strcat(location(k), '.csv');
tempfile = string(loc)
combinename = strcat (inFile,tempfile)
% Loading file
[A,B,C] = textread(combinename,'%*s %f %f %*f %f','headerlines',2,'delimiter',',','emptyvalue',NaN);
end
1 Comment
"I want it such that A1,B1,C1 is for the first area, A2,B2,C2 is for the second area and so on..."
Magically changing/accessing variable names is one way that beginners force themselves into writing slow, complex, buggy code. Read this to know why:
You should just use indexing and some cell arrays. Indexing is simple, neat, and very efficient. Unlike what you are trying to do. Indexing is exactly what the MATLAB documentation shows:
Accepted Answer
More Answers (0)
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!