struct problem when dealing with different field types
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Art
am 10 Apr. 2015
Bearbeitet: per isakson
am 16 Mai 2015
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 Kommentare
James Tursa
am 10 Apr. 2015
See Per's solution using the { } wrapper for the cell array of strings. E.g., for your case:
mystruct = struct('field1',variable1, ... ,'fieldX',{variableX})
Akzeptierte Antwort
per isakson
am 10 Apr. 2015
Bearbeitet: per isakson
am 10 Apr. 2015
"implement a cell in a struct"   I'm not sure I understand.
Hint:
>> sas = struct('vec',[1:12],'string','abcdefgh','cellstr',{{'abc123','def456'}})
sas =
vec: [1 2 3 4 5 6 7 8 9 10 11 12]
string: 'abcdefgh'
cellstr: {'abc123' 'def456'}
>> sas.structure = struct('f1',1,'s1','a' )
sas =
vec: [1 2 3 4 5 6 7 8 9 10 11 12]
string: 'abcdefgh'
cellstr: {'abc123' 'def456'}
structure: [1x1 struct]
>>
9 Kommentare
James Tursa
am 10 Apr. 2015
Bearbeitet: James Tursa
am 10 Apr. 2015
Well, you could find out if varname is a cell array and then take appropriate steps as you see fit. E.g.,
var_is_cell = eval(['iscell(' varname ')']);
if( var_is_cell )
varadd = ['{' varname '}'];
else
varadd = varname;
end
struct_construct = strcat(struct_construct,sprintf('''%s'',%s,',varname,varadd));
Or you could put in code to actually change the variable in question to a scalar cell array:
if( var_is_cell )
eval([varname '={' varname '}']);
end
But this is getting ugly, isn't it? Are you sure you want to go down this route of generating struct's with fieldnames that are defined from strings in real-time? How are you planning on dealing with this downstream in your code? More sprintf's and eval's? This is going to be a real headache down the road for you to manage the code for readability and maintainability.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!