Assign value to variables

Hello,
I have a cell array with char: c={'R1', 'R2', 'R3','R4'}
and I have a vector b=[1 0 0 1]
how can i assign values for the elements of c, so whenever I type R1 in the workspace I get the value =1;
(note that I need this to run a simulink file)

1 件のコメント

Stephen23
Stephen23 2021 年 4 月 30 日
"I have a cell array with char..."
But your example data here shows a non-scalar structure. Probably this does what you need:

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

回答 (2 件)

Steven Lord
Steven Lord 2021 年 4 月 30 日

0 投票

Can you define variables with numbered names like X1, X2, X3, ... ? Yes.
Consider instead one of the other approaches, like a model workspace, described in this section of the documentation.

3 件のコメント

fadi awar
fadi awar 2021 年 4 月 30 日
ck =
struct with fields:
R1R1: 1
R1R3: 1
R2R2: 0
R2R3: 0
R3R1: 1
R3R2: 0
R3R6: 0
R3R4: 1
R4R3: 1
R6R3: 0
R4R4: 1
R6R6: 0
R5R5: 0
R4R5: 0
R5R4: 0
R5R6: 0
R6R5: 0
I managed to store the data in a table, yet I need to get these values to workspace
For example after runing my mfile, if I write R6R5 i get 0 as answear, is it doable?
Steven Lord
Steven Lord 2021 年 4 月 30 日
As I said you can do this but you should not. See the Answers post to which I linked for a more thorough explanation of why this is discouraged and (if you feel you must do this) how you would.
fadi awar
fadi awar 2021 年 4 月 30 日
ok thank you sir, I'll check it

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

Scott MacKenzie
Scott MacKenzie 2021 年 4 月 30 日

0 投票

I'm not sure if this is helpful, but is seems to address the core issue you describe:
c={'R1', 'R2', 'R3', 'R4'};
b=[1 0 0 1];
z = input('Enter: ', 's');
b(strcmp(c,z)) % retrieve element from b in same position as user input in c
Sample run: (user input underlined)
Enter: R1
ans =
1
This isn't Simulink. It's just MATLAB code and Command Window interaction.

カテゴリ

ヘルプ センター および File ExchangeWorkspace Variables and MAT Files についてさらに検索

質問済み:

2021 年 4 月 30 日

コメント済み:

2021 年 4 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by