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

1 visualizzazione (ultimi 30 giorni)
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.

Risposte (1)

Sudhakar Shinde
Sudhakar Shinde il 27 Ott 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 Commento
Leon
Leon il 27 Ott 2020
Modificato: Leon il 27 Ott 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.

Accedi per commentare.

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by