Indexing large dataset in for loop and renaming
Show older comments
I'm trying to index a large data set by unique Identifier, rename as that identifier, and save file in a loop. I've done this before. It has worked before. I can't for the life of me get it to work now on this one certain file format and I don't understand why!!!!! Well... I kinda do but again it has worked in the past.
nn = ('Chptnk');
tgtt.Lat = Lat;
tgtt.Lon = Lon;
tgtt.vid = vid;
tgtt.T = T;
vesid = unique(tgtt.vid); % find ids
for i = 1:length(vesid)
vesids {i} = vesid(i);
ind = find(vid == vesids{i});
eval(sprintf('%s_%d(:,1) = tgtt.vid(ind);', nn, vesids{i}))
eval(sprintf('%s_%d(:,2) = tgtt.T(ind);', nn, vesids{i}))
eval(sprintf('%s_%d(:,3) = tgtt.Lat(ind);', nn, vesids{i}))
eval(sprintf('%s_%d(:,4) = tgtt.Lon(ind);', nn, vesids{i}))
eval(sprintf('x = %s_%d;', nn, vesids{i}))
dlmwrite(sprintf('%s_%d.csv', nn, vesids{i}), x)
end
This code works for my other formats. The catch is vid is pure numeric.
This other file format the vid is a mix of letters and numbers... example: g565d62d, a55d666g, 66f584fv2, add5544, HY55842.... etc
I've done this code with mixed numbers and letters before just fine(I just changed the %d to %s). but just won't work now for this current format.
I changed up the ind..
ind = find(ismember(tgtt.vid,vesids{i}));
Got it to index but now it tells me :
Error using sprintf
Function is not defined for 'cell' inputs.
I tried converting to double as a hail mary but get nans. I tried converting to table and altering code to index table in loop but can't get it to index. But then I was probably not writing that code right either.
in the past I've gotten this code to work on water quality data by do this at the start of code:
vars = {'NH4','DO','SAL','NO23','PO4','DSI','CHLA','WTEMP'};
It's a mix of numbers and letters too! worked fine! This new data has over 300 unique names! I tried to replicate vars by writing this:
vesids {i} = vesid(i);
looks the same to me?!
and so here is where I'm stuck......
nn = ('Chptnk');
tgtt.Lat = Lat;
tgtt.Lon = Lon;
tgtt.vid = vid;
tgtt.T = T;
vesid = unique(tgtt.vid); % find ids
for i = 1:length(vesid)
vesids {i} = vesid(i);
ind = find(ismember(tgtt.vid, vesids{i}));
eval(sprintf('%s_%s(:,1) = tgtt.vid(ind);', nn, vesids{i}))
eval(sprintf('%s_%s(:,2) = tgtt.T(ind);', nn, vesids{i}))
eval(sprintf('%s_%s(:,3) = tgtt.Lat(ind);', nn, vesids{i}))
eval(sprintf('%s_%s(:,4) = tgtt.Lon(ind);', nn, vesids{i}))
eval(sprintf('x = %s_%s;', nn, vesids{i}))
dlmwrite(sprintf('%s_%s.csv', nn, vesids{i}), x)
end
6 Comments
Jan
on 26 Feb 2018
Bad. What a bad idea to create the variables dynamically by eval. See http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval .
@Jenn: what do you need the structure for? It seems to be totally superfluous in this code that you have shown us: why not just access the variables Lat, etc, directly, rather than making copies of all of your data?
Your code is quite inefficient with memory: you end up with four copies of the data in four separate variables (original variable, structure tgtt, magically named variable, and temporarily in matrix x), but there seems to be no advantage (that I can see) to replicating your data like this. It is recommended to keep data together as much as possible (because this makes processing it simpler), and that the number of copies of data should be minimized (to reduce memory consumption).
Also find is less efficient than logical indexing.
Jenn
on 26 Feb 2018
Jenn
on 26 Feb 2018
Jan
on 26 Feb 2018
It would not be polite to visit your old boss and hit him.
Whenever you get stuck and try to eval, visit the forum and ask the community. We love to show you better ways. Sometimes this demands for a restructuring of the code, but it is always worth to do so.
Jenn
on 26 Feb 2018
Accepted Answer
More Answers (0)
Categories
Find more on Time Series Objects 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!