How to first form a variable and then assign values to it?

1 vue (au cours des 30 derniers jours)
Leon
Leon le 27 Oct 2020
Modifié(e) : Leon le 27 Oct 2020
My variable can be anything like the below:
if case1
Var = Temperature
elseif case2
Var = Pressure
elseif case3
Var = Oxygen
...
end
I need to be able to assign a column data to whether Temperature, or Pressure, or Oxygen, etc., depending on the cases. For example, if Var = temperature, I want my table column be like this:
Temperature = [1; 5; 3; 7; etc.];
How do I use "Var" to write the code, so that it will do things like above?
Many thanks.

Réponses (1)

Sudhakar Shinde
Sudhakar Shinde le 27 Oct 2020
Switch ..case can be one option.
Var='Temperature';
switch Var
case 'Temperature'
Temperature=[1; 5; 3; 7];
T = table(Temperature);
case 'Pressure'
Pressure=[1; 5; 3; 7];
T = table(Pressure);
case 'Oxygen'
Oxy=[1; 5; 3; 7];
T = table(Oxy);
end
  1 commentaire
Leon
Leon le 27 Oct 2020
Modifié(e) : Leon le 27 Oct 2020
Thanks, but my real world solution is way more complicated. Under each of the case, I would have hundreds of lines of code repeating.
What I really hope to get is one way to specify this Variable, so that I do not need to repeat those same lines of code over and over.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by