How to make the cleaner smoother loop for the following example?
1 view (last 30 days)
Show older comments
d21a = DIC(d.station == 21); u = unique(d.depth(d.station == 21)); n = histc(d.depth(d.station == 21),u); uu = u(n>1);
for i = 1:size(uu,1) rv = d.depth(d.station == 21) == uu(i); me = nanmean(d21a(rv)); d21a(rv) = me; end
d21b = d.depth(d.station == 21); d21(:,1) = d21b; d21(:,2) = d21a; d21 = sortrows(d21,1);
d.variables all = 444x1
I want to be able to specify how many stations (21:28) and then create variables related to the station within a loop. For example, instead of writing d21a, I would like something sort of like ['d', num2str(st),'a'] = blah blah. So far I haven't been able to crack it.
as at the moment, I am copying this code 8 times, one for each station (21:28), for four variables. So in total, 32 copies of this code, and it's too long.
I have already looked at the 'eval' function and I can't get it to work via converting a string to a variable....I think it's impossible...
thanks in advance, Michael
2 Comments
Adam
on 23 Mar 2015
Edited: Adam
on 23 Mar 2015
Use a cell array (assuming that you can't just use a standard array - that would depend on exactly what DIC( d.station == 21 ) returns ). One variable is better than 8 and is accessible via indexing rather than convoluted use of eval every time you wish to do something with a specific station.
Stephen23
on 23 Mar 2015
Edited: Stephen23
on 23 Mar 2015
Don't create (or name) new variables like that. You should consider using a non-scalar structure instead, which lets you define the structure using indices like this:
d(k).variables = [...];
d(k).depth = ...
d(k).station = ...
for any k, where the structure is indexed just like any MATLAB array and it can be a vector or matrix...
Accepted Answer
Stephen23
on 23 Mar 2015
Edited: Stephen23
on 23 Mar 2015
Avoid creating dynamically named variables in MATLAB. This is poor practice as has been explained many times on this forum, and is not recommended by MATLAB themselves:
When you are a beginner it seems like a cunning and fast way to store information, but actually it is really bad practice to name your variables dynamically. MATLAB is also not intended for this kind of variable naming: if you continue to include data in the variable names then you will find yourself fighting many more of these battles against MATLAB.
However when you use more appropriate storage for your data (and meta-data) then you will suddenly find lots of MATLAB functions that do many useful operations for you, quickly and easily.
In your case a much more robust solution would be to use non-scalar structure, where you can include fields for each kind of data (e.g. Process type, Flow data, Temperature data, Notes, Units, etc), and the structure itself can be indexed just like any other MATLAB array, here shown with linear index k:
d(k).depth = [..];
d(k).station = [..];
d(k).(...) = [..];
Note that you can even define structure fieldnames dynamically, and that there are many functions that support working on structures and cell arrays, and can access these data easily, and they can also be used in vectorized code (which is something you need to learn about).
Non-scalar structures allow very easy access to the data, for example you can obtain all of the depth values in a comma separated list like this:
d.depth
which means you can create a cell array from all depth values like this:
{d.depth}
or concatenate all the depth data matrices like this:
[d.depth]
or use them all as inputs to a function like this:
vertcat(d.depth)
You can retrieve all of the fields and values of for one group of data simply using the index, e.g.:
d(9)
returns the scalar structure with all of the fields for the ninth data set, and
d(9).depth
gives the depth value of the ninth data set. The index can, just like any other MATLAB array, be logical, subscripts or linear indexing . so you can easily perform operations on a subset of your data like this:
myfunction(d([1,5,8]).station)
to input into some function the station values for data sets one, five and eight. You can also subset a non-scalar structure, just like you would any other array:
B = d(3:6)
assigns the subset of the original structure to the variable B. When you first create the structure, you should consider preallocating it so that it does not keep getting expanded in a loop.
Placing your data in a structure or cell array also makes it much easier to pass to functions: can you imagine the fight you would have trying to pass hundreds of dynamically named variables to a function?
In case you are interested, here are some pages explaining why dynamically assigning variable names is a really bad idea in MATLAB:
0 Comments
More Answers (0)
See Also
Categories
Find more on Variables 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!