Assign value to variables

6 visualizaciones (últimos 30 días)
fadi awar
fadi awar el 30 de Abr. de 2021
Comentada: Stephen23 el 30 de Abr. de 2021
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 comentario
Stephen23
Stephen23 el 30 de Abr. de 2021
"I have a cell array with char..."
But your example data here shows a non-scalar structure. Probably this does what you need:

Iniciar sesión para comentar.

Respuestas (2)

Steven Lord
Steven Lord el 30 de Abr. de 2021
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 comentarios
Steven Lord
Steven Lord el 30 de Abr. de 2021
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 el 30 de Abr. de 2021
ok thank you sir, I'll check it

Iniciar sesión para comentar.


Scott MacKenzie
Scott MacKenzie el 30 de Abr. de 2021
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.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by