select a character value to display depending on the value entered

4 visualizaciones (últimos 30 días)
I am trying to compress my code i have this currently the qo tells the script what values to ask for and assigns them to LoadType in the form [1,0,2,0,4,1] this is later used in a function with a switch and case. i am trying to compress the if LoadType(1) == 1; lt1 = string, etc. lt1 is later used in an fprintf statement. is there a way that a for loop could asign a string to lt1 depending on the value that is in LoadType(1). meaning if LoadType(1) is 1 is says constant or if its 3 it says Linear Ramp up. I am trying to do this for every spot that is greater then zero in the LoadType matrix. If i worded this weird Please ask for clarification.
qo = 2*[1,0,1,0,1,1];
LoadType = ones(size(qo));
for k = find(qo)
LoadType(k) = menu(sprintf('Choose %d-segment load function',k),...
'Constant load' ,'Constant load on bottom',...
'Linear ramp up','Linear ramp down' ,...
'Ramp up on Bottom','Ramp down on bottom',...
'Half sinusoid load','Half sinusoid load on bottom', ...
'Half cosine load','Half cosine load on bottom',...
'Gradual point load of section of segment',...
'Gradual point load of section of segment on bottom',...
'Section of segment with Load',...
'Section of segment with Load on bottom',...
'Constant load with a segment of 0',...
'Constant load with a segment of 0 on bottom',...
'Trapezoidal distribution');
if LoadType(1) == 1; lt1 = 'Constant';end
if LoadType(1) == 2; lt1 = 'Constant on below';end
if LoadType(1) == 3; lt1 = 'Linear ramp up';end
if LoadType(1) == 4; lt1 = 'Linear ramp down';end
if LoadType(1) == 5; lt1 = 'Linear ramp up on bottom';end
if LoadType(1) == 6; lt1 = 'Linear ramp down on bottom';end
if LoadType(1) == 7; lt1 = 'Half sinusoid load';end
if LoadType(1) == 8; lt1 = 'Half sinusoid load on bottom';end
if LoadType(1) == 9; lt1 = 'Half cosine load';end
if LoadType(1) == 10; lt1 = 'Half cosine load on bottom';end
if LoadType(1) == 11; lt1 = 'Gradual point load of section of segment';end
if LoadType(1) == 12; lt1 = 'Gradual point load of section of segment on bottom';end
if LoadType(1) == 13; lt1 = 'Section of segment with Load';end
if LoadType(1) == 14; lt1 = 'Section of segment with Load on bottom';end
if LoadType(1) == 15; lt1 = 'Constant load with a segment of 0';end
if LoadType(1) == 16; lt1 = 'Constant load with a segment of 0 on bottom';end
if LoadType(1) == 17; lt1 = 'Trapezoidal distribution';end
if LoadType(2) == 1; lt2 = 'Constant';end
if LoadType(2) == 2; lt2 = 'Constant on below';end
if LoadType(2) == 3; lt2 = 'Linear ramp up';end
if LoadType(2) == 4; lt2 = 'Linear ramp down';end
if LoadType(2) == 5; lt2 = 'Linear ramp up on bottom';end
if LoadType(2) == 6; lt2 = 'Linear ramp down on bottom';end
if LoadType(2) == 7; lt2 = 'Half sinusoid load';end
if LoadType(2) == 8; lt2 = 'Half sinusoid load on bottom';end
if LoadType(2) == 9; lt2 = 'Half cosine load';end
if LoadType(2) == 10; lt2 = 'Half cosine load on bottom';end
if LoadType(2) == 11; lt2 = 'Gradual point load of section of segment';end
if LoadType(2) == 12; lt2 = 'Gradual point load of section of segment on bottom';end
if LoadType(2) == 13; lt2 = 'Section of segment with Load';end
if LoadType(2) == 14; lt2 = 'Section of segment with Load on bottom';end
if LoadType(2) == 15; lt2 = 'Constant load with a segment of 0';end
if LoadType(2) == 16; lt2 = 'Constant load with a segment of 0 on bottom';end
if LoadType(2) == 17; lt2 = 'Trapezoidal distribution';end
and so on

Respuesta aceptada

the cyclist
the cyclist el 1 de Nov. de 2021
I believe this will do what you want.
Define the cell array
lt_list = {'Constant','Constant on below' ... etc
then
lt1 = lt_list(loadType(1));
lt2 = lt_list(loadType(2));
  3 comentarios
Joseph Catanese
Joseph Catanese el 2 de Nov. de 2021
how do i define it as 'constant' rather then a cell aray thats the problem
the cyclist
the cyclist el 2 de Nov. de 2021
Use curly brackets to access the contents of the cell, rather than the cell itself.
lt1 = lt_list{loadType(1)};

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by