Wildcard in variable name or changing variable names in loop
Show older comments
Hi!
I have a question, I already found some answers but not really applicable for my problem.
I got a lot of code where I calculate with different variables. The input are monthly values, every month the same variables but not the same values ofcourse. The calculations remain the same. Now I made variables like: jan_total, feb_total, mar_total. But I like to loop the calculations like this, where in every loop '#' is replaced by e.g. 'jan', 'feb', 'mar';
#_vent_array4 = reshape(#_FlowZone4, [reshape_interval, reshape_data]);
#_FlowZone4day = zeros(reshape_data,1);
for a= 0: (reshape_data-1)
#_FlowZone1day(a+1)= mean(#_vent_array1((1+(a*reshape_interval)):(reshape_interval+(a*reshape_interval))));
#_FlowZone2day(a+1)= mean(#_vent_array2((1+(a*reshape_interval)):(reshape_interval+(a*reshape_interval))));
end
#_deltaTzone4 = #_TempZone4day - #_KNMITempday;
#_deltaTzone5 = #_TempZone5day - #_KNMITempday;
#_QTRwindows = qTRwindows * #_deltaT_mean * (3600 * 24) / (10^6 * 3.6);
#_QTRwalls = (qTRroof+qTRfloor+qTRwalls) * #_deltaT_mean * (3600*24)/(10^6*3.6);
In this way I can't make any mistakes copying code if I change little things in the calculations.
Can someone help me?
**** edit 21/2/2016:
Thank you very much for all the answers! I was indeed searching for the structure arrays. Now I got a worksheet were I name all the variables, here I replaced 'jan_', 'feb_', 'mar_' with month(1). month(2), month(3). what makes it possible to loop the whole code like I want to. is it also possible to use ALL data like this? For example the whole year, or a quartile?
month.variable
does not do the trick.
4 Comments
per isakson
on 21 Feb 2016
Edited: per isakson
on 21 Feb 2016
Don't do this. Use more robust syntaxes, such as a simple indexed variables or a structure instead. Eg to define:
>> total.('jan') = 1;
>> total.('feb') = 2;
>> total.('mar') = 3;
and then to access is equally simple:
>> total.('jan')
ans =
1
And looping over them is easy:
C = {'jan','feb','mar'};
for k = 1:numel(C)
total.(C{k})
end
This is much more robust, faster, and neater than trying to mess around with awful, hacky, buggy dynamic variable names. Although you imagine that by using dynamic variable names "I can't make any mistakes copying code" in fact these are the cause of beginners bugs that are almost impossible to debug. Dynamic variable names result in obfuscated, buggy, and slow code: why bother?
There is even the issue of putting meta-data in the variable names: this breaks the idea of code being generalized. Meta-data deserves to be treated as data in its own right.
Jan
on 21 Feb 2016
Do not use automagically expanded names. Such meta programming is a really bad style.
loes7815
on 21 Feb 2016
Accepted Answer
More Answers (1)
Image Analyst
on 21 Feb 2016
You can put all that into a function and just call the function 12 times with different output names, if you really want separate variables rather than an array. I'm not sure what your output variables are but let's say that they are QTRwindows and QTRwalls. So then you'd pass in the input arguments and carry out all your computations without the #_ before the names. Like this:
function [QTRwindows, QTRwalls] = ComputeValues(your, inputValues, go, here)
% Code without #_
Then assign them in the main calling routine:
[jan_QTRwindows, jan_QTRwalls] = ComputeValues(.......
[feb_QTRwindows, feb_QTRwalls] = ComputeValues(.......
[mar_QTRwindows, mar_QTRwalls] = ComputeValues(.......
[apr_QTRwindows, apr_QTRwalls] = ComputeValues(.......
[may_QTRwindows, may_QTRwalls] = ComputeValues(.......
[jun_QTRwindows, jun_QTRwalls] = ComputeValues(.......
[jul_QTRwindows, jul_QTRwalls] = ComputeValues(.......
[aug_QTRwindows, aug_QTRwalls] = ComputeValues(.......
[sep_QTRwindows, sep_QTRwalls] = ComputeValues(.......
[oct_QTRwindows, oct_QTRwalls] = ComputeValues(.......
[nov_QTRwindows, nov_QTRwalls] = ComputeValues(.......
[dec_QTRwindows, dec_QTRwalls] = ComputeValues(.......
Doing this a few times is quite normal. Doing it 12 times is getting up there but can be done, though for 12 I'd prefer an array, which gives you the benefit of being able to refer to the results with a month number rather than having to use something like 'if' or 'switch' to make sure you refer to the proper variable.
Categories
Find more on Loops and Conditional Statements 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!