Getting data from structure

4 次查看(过去 30 天)
Ryan0101
Ryan0101 2012-2-28
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 个评论
Walter Roberson
Walter Roberson 2012-2-28
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.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2012-2-29
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 个评论
Jan
Jan 2012-2-29
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 2012-2-29
ahhh you da man Jan. thanks for the help.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by