Concatenating strings w/ character with while loops

I need to concatenate the elements of firstname until it contains the whole first name(nate) of myname.
myname = 'Nate Johnson';
firstname = '';
length(myname)
f = strcat(myname, g);
while length(myname) < 7
firstname = myname;
x = myname;
end
disp(x)

1 comentario

while length(myname) < 7
firstname = myname;
x = myname;
end
Inside your loop, you are not changing myname, so if your loop starts going at all, your loop will never end.

Iniciar sesión para comentar.

 Respuesta aceptada

dpb
dpb el 9 de Feb. de 2020
Editada: dpb el 9 de Feb. de 2020
>> myname = 'Nate Johnson';
>> split(myname)
ans =
2×1 cell array
{'Nate' }
{'Johnson'}
>>
presuming the next step would be to also isolate lastname.
Or, alternatively
>> firstname=extractBefore(myname,' ')
firstname =
'Nate'
>>

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 9 de Feb. de 2020

Editada:

dpb
el 9 de Feb. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by