How to create dynamic field reference in a structure

14 ビュー (過去 30 日間)
Udayteja
Udayteja 2012 年 6 月 18 日
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..

採用された回答

Jan
Jan 2012 年 6 月 18 日
Perhaps Marker_Name is a scalar cell string, then:
Req.info(i).Complete_Data.(Marker_name{1}) = Marker_data;
  4 件のコメント
Udayteja
Udayteja 2012 年 6 月 18 日
Hi,
After certain iterations the program stops with the following error.
Index exceeds matrix dimensions.
Is there a way i could increase the size?
Walter Roberson
Walter Roberson 2012 年 6 月 18 日
"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.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 6 月 18 日
Req.info(i).Complete_Data.(Marker_name) = Marker_data;
  2 件のコメント
Udayteja
Udayteja 2012 年 6 月 18 日
Thanq for your reply. But even this is showing an error.
??? Argument to dynamic structure reference must evaluate to a valid field name.
Walter Roberson
Walter Roberson 2012 年 6 月 18 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTesting Frameworks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by