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

1 回表示 (過去 30 日間)
Leon
Leon 2020 年 10 月 27 日
編集済み: Leon 2020 年 10 月 27 日
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.

回答 (1 件)

Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 27 日
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 件のコメント
Leon
Leon 2020 年 10 月 27 日
編集済み: Leon 2020 年 10 月 27 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by