How to create dynamic field reference in a structure

Hi,
I am trying to read lines from a text file generated during a simulation. For ex
Text file has information as follows
$------------------------------------------------------------------------REQUEST
[REQUEST]
NAME = 'driver_demands'
ID = 1
X = 'steering'
Y = 'throttle'
Z = 'brake'
R1 = 'gear_position'
R2 = 'clutch'
X_UNITS = 'angle'
Z_UNITS = 'force'
$------------------------------------------------------------------------REQUEST
[REQUEST]
NAME = 'sse_outputs'
ID = 2
X = 'yaw_moment'
Y = 'lateral_force'
X_UNITS = 'torque'
Y_UNITS = 'force'
$------------------------------------------------------------------------REQUEST
I would like to create a structure with dynamic field names. Req.info is a structure with 4 fields ID, NAME, Marker_Data, Complete_Data
I would like to update Req.info Such that Req.info(1).Complete_Data =
NAME: 'driver_demands'
ID: 1
X: 'steering'
Y: 'throttle'
Z: 'brake'
R1: 'gear_position'
R2: 'clutch'
X_UNITS: 'angle'
Z_UNITS: 'force'
Where
Req.info(1).Complete_Data.X
Req.info(1).Complete_Data.Y
Req.info(1).Complete_Data.Z... (Fields X,Y,Z,.... are created dynamically)
Such that Req.info(1).Complete_Data.X = steering and so on...
I have tried with following code but could not succeed.
while ~strncmp(zeile,'$', 1)
line = fgetl(fid);
[Marker_name, Marker_data]= strread(line, '%s''%s', 'delimiter','=');
Marker_name = strtrim(Marker_name);
Marker_data = strtrim(Marker_data);
Marker_data = Marker_data (1:end-1);
eval(['Req.info(', num2str(i), ').Complete_Data.', char(Marker_name), ' = ', char(Marker_name), ';']);
end
This is the first time am dealing with data import functions. Please help me out..
Thanx a lot..

 Réponse acceptée

Jan
Jan le 18 Juin 2012
Perhaps Marker_Name is a scalar cell string, then:
Req.info(i).Complete_Data.(Marker_name{1}) = Marker_data;

4 commentaires

Udayteja
Udayteja le 18 Juin 2012
Thanx a lot...:)
Now the code runs giving following result
Req.info(1).Complete_Data
ans =
X: {'steering''}
Y: {'throttle''}
Z: {'brake''}
R1: {'gear_position''}
R2: {'clutch''}
X_UNITS: {'angle''}
Z_UNITS: {'force''}
but I do not understand the significance of flower braces here. what do you mean by scalar cell string. How can I change the result such that I do not get the Flower braces and an additional ' at the end of each line?
Req.info(i).Complete_Data.(Marker_name{1}) = Marker_data{1};
Udayteja
Udayteja le 18 Juin 2012
Hi,
After certain iterations the program stops with the following error.
Index exceeds matrix dimensions.
Is there a way i could increase the size?
"index exceeds matrix dimensions" is always a programming error, not a problem with the size being too small.
The first thing I would check at that point is whether at some point the strread() is returning empty cell arrays -- if it was then element {1} would exceed the dimensions of the array.

Connectez-vous pour commenter.

Plus de réponses (1)

Req.info(i).Complete_Data.(Marker_name) = Marker_data;

2 commentaires

Udayteja
Udayteja le 18 Juin 2012
Thanq for your reply. But even this is showing an error.
??? Argument to dynamic structure reference must evaluate to a valid field name.
At the line before that,
fprintf(1, '|%s|\n', Marker_name);
length(Marker_name)
and check to see whether Marker_name has any blanks or any characters other than A-Z, a-z, 0-9, and underscore. If it has any of those, then it is not a valid name for a structure field.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Testing Frameworks dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by