Getting data from structure

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.
Ryan0101
Ryan0101 2012년 2월 29일
2011b

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2012년 2월 29일

2 개 추천

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

Ryan0101
Ryan0101 2012년 2월 29일
Thanks for the response.
I tried both ways.
The RenameField way I got the error -
error using cell2struct
invalid field name "Model Root.."
The newName way -
error using subsindex
function 'subsindex' is not defined for values of class 'cell'
again thanks for helping out
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개)

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

질문:

2012년 2월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by