Retrieving strings from struct variable

9 ビュー (過去 30 日間)
James
James 2011 年 7 月 20 日
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?

採用された回答

Sean de Wolski
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}

その他の回答 (2 件)

Fangjun Jiang
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

James
James 2011 年 7 月 20 日
Ah, it seems my research should have turned towards cells rather than structures. Thanks for the response!
--Jimmy

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by