Getting data from structure

4 views (last 30 days)
Ryan0101
Ryan0101 on 28 Feb 2012
Hello, I'm using dSPACE to output the results to Matlab. The data comes out as a structure with the variables nested inside. That is fine, but the variable names have spaces in them and I'm not sure how to get the data. And no I cannot change the name of the variables because that is what dSpace/Simulink assign them.
It looks like this:
>> A
A = Platform_HostService: [1x1 struct]
>> A.Platform_HostService
ans =
xAxis: [1x1000 double]
Model Root/Pulse_Generator/Out1: [1x1000 double]
... and so on.
I can't write A.Platform_HostService.Model Root... how do take care of the white space?
Thanks
  2 Comments
Walter Roberson
Walter Roberson on 28 Feb 2012
There is a hack for this, but I do not recall at the moment whether James or Jan maintain the code.
Which MATLAB version are you using? The easy of hacking it depends on the MATLAB version.

Sign in to comment.

Accepted Answer

Jan
Jan on 29 Feb 2012
You can either try to use dynamic field names:
A.Platform_HostService.('Model Root/Pulse_Generator/Out1')
or use FEX: RenameField to rename the fields:
S = A.Platform_HostService;
List = fieldnames(S);
for i = 1:length(List)
S = RenameField(S, List{i}, genvarname(List{i}));
end
Afterwards S is clean. Instead of genvarname you can use this also:
Name = List{i};
newName = Name(isstrprop(Name, 'alphanum'));
[EDITED] Clean all names at first if you use the M-code fallback of RenameField:
S = A.Platform_HostService;
old = fieldnames(S);
new = cell(size(old));
for i = 1:length(old)
new = genvarname(old{i}); % Or the ISSTRPROP method
end
S = RenameField(S, old, new)
Or:
S = A.Platform_HostService;
old = fieldnames(S);
for i = 1:length(old)
new = genvarname(old{i}); % Or the ISSTRPROP method
T.(new) = S.(old{i});
end
  3 Comments
Jan
Jan on 29 Feb 2012
Please follow the instructions in RenameField to compile the C-Mex function. The CELL2STRUCT error means, that you are using the less powerful fallback in M-code. I do not understand how you used the "The newName way" to obtain the posted error. The first method I've posted was the dynamic field names (does it work?) and RenameField. For the last I gave two examples of how the automatic clean up of the name might be implemented - by genvarname or using isstrprop.
Anyhow, the CELL2STRUCT error seems to imply, that even the M-code RenameField works, if you clean up all names at first. See [EDITED].
Ryan0101
Ryan0101 on 29 Feb 2012
ahhh you da man Jan. thanks for the help.

Sign in to comment.

More Answers (0)

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!