Incorrect Logical Condition Statement

1 回表示 (過去 30 日間)
Jake Bowd
Jake Bowd 2020 年 6 月 16 日
コメント済み: KSSV 2020 年 6 月 16 日
Hi,
I have a variable (let's call it 'data') and dependent on whether the user is left ('L) or right ('R') footed I wish for the respective data to be called.
I.e. for if 'L' then I want ('data') to be: 'data_l'
If 'R' is selected then I want ('data') to be: 'data_r'
Essentially I wish to add in the respective 'l' or 'r' to the end of the name in accordance to whether the person is left or right sided.
Leg = 'L'; %change between L and R
if Leg == 'L' ;
LEG = 'l';
else Leg == 'R';
LEG = 'r';
end
Tables.knee_flex_LEG % would like LEG to change to 'l' or 'r' accordingly
Unrecognized table variable name 'knee_flex_LEG'.
I believe the if function is the one I need to use. However I am unsure how to add the option into the script
  1 件のコメント
KSSV
KSSV 2020 年 6 月 16 日
Don't use
if Leg == 'L'
use
if strcmp(Leg,'L')

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

採用された回答

KSSV
KSSV 2020 年 6 月 16 日
Leg = 'L'; %change between L and R
if strcmp(Leg,'L') ;
LEG = 'l';
else strcmp(Leg,'R');
LEG = 'r';
end
id = 'knee_flex_LEG' ;
id = strrep(id,'LEG',LEG)
  2 件のコメント
Jake Bowd
Jake Bowd 2020 年 6 月 16 日
Hi KSSV,
This works great and thank you for that.
However, what would you recommend I do if for example it isn't just the 'knee_flex_LEG' variable and infact there's more i.e. also a 'knee_add_LEG' and 'knee_rot_LEG' variables.
If I am only concerned with the Right variables is there a way to apply 'r' ti the end of each variable?
KSSV
KSSV 2020 年 6 月 16 日
You can replace all the variables at once using strrep.
str{1} = "knee_add_LEG" ;
str{2} = "knee_rot_LEG" ;
str = strrep(str,'Leg',LEG)

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

その他の回答 (1 件)

Stephen23
Stephen23 2020 年 6 月 16 日
編集済み: Stephen23 2020 年 6 月 16 日
Leg = 'L'; %change between L and R
vnm = sprintf('knee_flex_%s',lower(Leg));
Tables.(vnm)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by