Concatenating strings w/ character with while loops

1 vue (au cours des 30 derniers jours)
Nathen Eberhardt
Nathen Eberhardt le 9 Fév 2020
Modifié(e) : dpb le 9 Fév 2020
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 commentaire
Walter Roberson
Walter Roberson le 9 Fév 2020
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.

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 9 Fév 2020
Modifié(e) : dpb le 9 Fév 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'
>>

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by