struct problem when dealing with different field types
古いコメントを表示
I created a struct containing variables of length 'n' (numeric) in separate fields: mystruct = struct('field1',variable_1,'field2', variable_2, ...)
everything works fine and I can type mystruct.field1 and it shows the values of variable_1 from 1:n:
ans =
32 2 22 1 1 2 ...n
If I type mystruct.field1(1) I get 32. This is good.
Now I want to include a variable which is a character cell in the struct, also of length n (n cells of differing sizes). When I add this variable to the struct, all the fields change to cells (I think), and I have to type mystruct (1).field1(1) to see 32. This is bad.
Is there a way to implement a cell in a struct without changing everything to cell? Or, is there a way to make a char string into something besides a cell to be stored into struct?
I tried changing the ascii chars into a binary stream and using bin2dec to turn into the dec equivalent for use in the struct, the problem is that this only works for shorter strings. If the string is too long, the decimal equivalent is too big for Matlab to handle.
Any ideas?
3 件のコメント
James Tursa
2015 年 4 月 10 日
Please post the exact code you used to "... add this variable to the struct ..."
You can certainly add a cell array to a struct without changing the struct itself to a cell array by simply putting it under a different field name. But we need to see your code in order to advise you how to correct things.
Art
2015 年 4 月 10 日
編集済み: per isakson
2015 年 5 月 16 日
James Tursa
2015 年 4 月 10 日
See Per's solution using the { } wrapper for the cell array of strings. E.g., for your case:
mystruct = struct('field1',variable1, ... ,'fieldX',{variableX})
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!