Concatenating strings w/ character with while loops

1 Ansicht (letzte 30 Tage)
Nathen Eberhardt
Nathen Eberhardt am 9 Feb. 2020
Bearbeitet: dpb am 9 Feb. 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 Kommentar
Walter Roberson
Walter Roberson am 9 Feb. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 9 Feb. 2020
Bearbeitet: dpb am 9 Feb. 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'
>>

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by