How to Use Character String as a For Loop Variable.

3 visualizaciones (últimos 30 días)
johnsmithofmatlab
johnsmithofmatlab el 5 de Ag. de 2018
Editada: jonas el 5 de Ag. de 2018
I am attempting to use a character string as a variable in the following for loop:
Spring_Type = "Closed and Ground";
if Spring_Type = "Open";
Pitch = (Free_Length - D_Wire)/A_Coils;
elseif Spring_Type = "Open and Ground";
Pitch = Free_Length/Tot_Coils;
Pitch = (Free_Length - (2*D_Wire))/A_Coils;
elseif Spring_Type = "Closed";
Pitch = (Free_Length - (3*D_Wire))/A_Coils;
elseif Spring_Type = "Closed and Ground";
Pitch = (Free_Length - (2*D_Wire))/A_Coils;
end
All the other variables are fully defined in a section before this. When I run this section I get the following error:
Could someone explain why I cannot use a character string as a variable, and if there is an alternative way to achieve what I am doing?
Many thanks,

Respuesta aceptada

jonas
jonas el 5 de Ag. de 2018
Editada: jonas el 5 de Ag. de 2018
The condition must output a logical. Use e.g. isequal or ==
if isequal(Spring_type,'Open')
...
end
or
if Spring_type == 'Open'
...
end

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by