Wildcard in variable name or changing variable names in loop

4 visualizzazioni (ultimi 30 giorni)
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 Commenti
Jan
Jan il 21 Feb 2016
Do not use automagically expanded names. Such meta programming is a really bad style.
loes7815
loes7815 il 21 Feb 2016
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?

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 21 Feb 2016
Modificato: Stephen23 il 28 Giu 2019
  2 Commenti
loes7815
loes7815 il 21 Feb 2016
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?
Stephen23
Stephen23 il 21 Feb 2016
Modificato: Stephen23 il 21 Feb 2016
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.

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 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.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by