Retrieving strings from struct variable
조회 수: 9 (최근 30일)
이전 댓글 표시
Hello,
What I am trying to do is retrieve strings from a struct variable and store them in a vector. The struct variable is 16x1 with the first field holding the string data.
Here is what I tried, for example:
---------------
for i = 1:16
stringVector(i) = structVar(i).stringField;
end
---------------
Here is the error I am receiving:
"??? In an assignment A(:) = B, the number of elements in A and B must be the same."
Any thoughts?
댓글 수: 0
채택된 답변
Sean de Wolski
2011년 7월 20일
stringvector should be a cell
stringvector = cell(16,1)
for ii = 1:16
stringvector{ii} = structVar(ii).stringField
end
You could also skip the for-loop with
stringvector = {structVar.stringfield}
댓글 수: 0
추가 답변 (2개)
Fangjun Jiang
2011년 7월 20일
Your strings may have different length. Thus, it is better to use cell array of strings. change your line to be:
stringVector{i} = structVar(i).stringField
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!