How can I put data into a Cell

I have a cell Name = {} And i need to make a list of names using a loop. How do I do this without overwriting ? Thanks

답변 (2개)

Adam
Adam 2017년 3월 23일
편집: Adam 2017년 3월 23일

0 개 추천

for n = 1:numNames
Name{n} = getYourNameFromSomewhere( n );
end
Doing it in a loop implies your names are somewhere where you can assign them without needing to hard code them. Whether that is the case or not I don't know so the above is kind of pseudo-code

댓글 수: 3

And for completeness: Add a pre-allocation on top:
Name = cell(1, numNames);
dpb
dpb 2017년 3월 25일
[ Anne's Answer moved to comment as is followup ?-- dpb]
thank You. But i get a lot of [] [] [] []
dpb
dpb 2017년 3월 25일
"Show your work!" We can't debug what we can't see...need the code you used and a sample of the data you tried to assign.

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

Anne
Anne 2017년 3월 23일

0 개 추천

thank You. But i get a lot of [] [] [] []

댓글 수: 1

Adam
Adam 2017년 3월 23일
Well, it depends entirely on how you get hold of all your names in a loop. Using Jan's code to initialise the array that is what you will start with. If your code in the for loop fails to assign anything then you will end up with that still too.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2017년 3월 23일

댓글:

dpb
2017년 3월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by