How to loop through variable names and to save them on one file?

11 vues (au cours des 30 derniers jours)
JMSE
JMSE le 4 Août 2021
Commenté : JMSE le 5 Août 2021
Hi, I have written this loop which works fine, however I want to perform this for certain variables in the data_measured csv file. Currently, this loop performs well for the WD__Avr variable. Hence, I would like to define the variable = x before the loop and just replace it with "t.'variable" within the loop. Any suggestion?
Thanks for your help!
for i = 1:height(unique(data_timesteps.Date))
ind_dt = find(data_timesteps.Date == dates(i));
t = table(data_timesteps.Time(ind_dt), zeros(sum(data_timesteps.Date == dates(i)),1));
header={'Time','WD_Avr'};
t.Properties.VariableNames = header;
for j = 1:(sum(data_timesteps.Date == dates(i)))
if j == 1
ind_time = find(data_measured.Time <= data_timesteps.Time(j) & data_measured.Date == dates(i));
ind_time = ind_time(end-10+1:end);
x = mean(data_measured.WD__Avr(ind_time));
t.WD_Avr(j) = x;
data_timesteps.WD(ind_dt) = t.WD_Avr;
else
ind_time = find(data_measured.Time <= data_timesteps.Time(j) & data_measured.Time > data_timesteps.Time(j-1) & data_measured.Date == dates(i));
x = mean(data_measured.WD__Avr(ind_time));
t.WD_Avr(j) = x;
data_timesteps.WD(ind_dt) = t.WD_Avr;
end
end
end
  2 commentaires
Stephen23
Stephen23 le 5 Août 2021
Modifié(e) : Stephen23 le 5 Août 2021
The different ways to access table variables/columns is explained here:
JMSE
JMSE le 5 Août 2021
Thanks!

Connectez-vous pour commenter.

Réponse acceptée

Jeff Miller
Jeff Miller le 5 Août 2021
You can use strings for variable names like this:
a = 'WD__Avr';
for....
[...]
x = mean(data_measured.(a)(ind_time)); % note parentheses around 'a'
[...]
...end
You might make a cell array with the different strings you want for 'a' and then loop through assigned each one to 'a' to process the different variables.
  1 commentaire
JMSE
JMSE le 5 Août 2021
Perfect, that's what I have been looking for. Many thanks!

Connectez-vous pour commenter.

Plus de réponses (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 4 Août 2021
It looks that will require another loop outside the main loop, something like this one, e.g.:
for i = 1:height(unique(data_timesteps.Date))
ind_dt = find(data_timesteps.Date == dates(i));
x.Time(i) = data_timesteps.Time(ind_dt);
end
  1 commentaire
JMSE
JMSE le 4 Août 2021
Thank you very much. Unfortunately, I cannot see how this solves my issue. Actually I only want to name the table.variable in such a manner that 'variable' changes through the loops.
I have tried different versions of:
a = WD__Avr
for....
[...]
x = mean(data_measured.a(ind_time));
[...]
...end
which does not assign work. This might be quite blatant; I am sorry - quite new to matlab.
Thanks!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by