Getting data from structure

4 vues (au cours des 30 derniers jours)
Ryan0101
Ryan0101 le 28 Fév 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 commentaires
Walter Roberson
Walter Roberson le 28 Fév 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.
Ryan0101
Ryan0101 le 29 Fév 2012
2011b

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 29 Fév 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 commentaires
Jan
Jan le 29 Fév 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 le 29 Fév 2012
ahhh you da man Jan. thanks for the help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by