Creatimg a strucure in two cases of a switch block
2 views (last 30 days)
Show older comments
I have a problem with a data structure in matlab. Hope you can help. This what i want to be done..
there is a structure (the following one). I haven't declared it, but I'm assigning values to it on the go, in a switch block. The fields are filled in different switch blocks.
GridMapSetInfo =
atom_types: {6x1 cell}
num_atom_types: 6
spacing: 0.3750
num_points: [1x1 struct]
centre: [1x1 struct]
receptor: 'hsg1.pdbqt'
switch A
case 'ligand_types'
y =line(13:length(line)-24);
atom_array= strread(y, '%s');
GridMapSetInfo.atom_types=atom_array; GridMapSetInfo.num_atom_types=length(atom_array);
case 'fld'
fld = sscanf(line, '%*s %s %**');
GridMapSetInfo = readFLD(fld,GridMapSetInfo);
%disp(GridMapSetInfo.atom_type); -----ERROR cannot access
Problem is that, the fields that I assign in one case block cannot be accessed in the other case block. In a language like java this won't happen because the structure is declared with all its fields before assigning values.
Can you suggest a way to do this?
0 Comments
Answers (1)
Walter Roberson
on 15 Aug 2012
It doesn't matter that the fields are not accessible in the other case, as you are only executing one case. If you were to enclose the code in a loop that worked through the different cases then you would find that the fields that had been assigned do exist.
In your code, logically you are attempting to access something that has never been defined. Even if the field were to somehow come into existence by being assigned in the other case, the field would not have a useful value.
If you have a field whose value needs to be defined in all of the cases, you need to either assign it a value in all of the cases, or you need to assign the default value for it before the switch()
0 Comments
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!