Wildcard in variable name or changing variable names in loop

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

Have you seen this advise, Please don't do this!?
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.
Do not use automagically expanded names. Such meta programming is a really bad style.
Thank you very much! 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?

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 21 Feb 2016
Edited: Stephen23 on 28 Jun 2019

2 Comments

Thank you very much! I wasn't planning on doing something like that, 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?
The simplest data is the best data storage. As long as it is obvious (e.g. month(1), month(2)) or you keep a separate array of the values, then you can use indexing to store any sequence of values, such as year or quartile data.
You could index a year by:
  • counting from the 1st of Jan.
  • using the ISO 8601 day of the year
  • store a corresponding array of dates
There is no one solution: you can pick the solution that best fits your data and your data processing needs. You might also like to consider a table for storing your data.

Sign in to comment.

More Answers (1)

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

Asked:

on 21 Feb 2016

Edited:

on 28 Jun 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!