Concatenating strings w/ character with while loops

조회 수: 1 (최근 30일)
Nathen Eberhardt
Nathen Eberhardt 2020년 2월 9일
편집: dpb 2020년 2월 9일
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
Walter Roberson
Walter Roberson 2020년 2월 9일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2020년 2월 9일
편집: dpb 2020년 2월 9일
>> 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'
>>

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by