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

1 view (last 30 days)
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.

Answers (1)

Sudhakar Shinde
Sudhakar Shinde on 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 Comment
Leon
Leon on 27 Oct 2020
Edited: Leon on 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.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!