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

1 visualización (últimos 30 días)
Leon
Leon el 27 de Oct. de 2020
Editada: Leon el 27 de Oct. de 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.

Respuestas (1)

Sudhakar Shinde
Sudhakar Shinde el 27 de Oct. de 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 comentario
Leon
Leon el 27 de Oct. de 2020
Editada: Leon el 27 de Oct. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by