Access to a structure value with the field path in a String form

32 views (last 30 days)
Hi,
I don't know how to access to a specific array stored in a structure by using the field path stored in a String form. The following code shows my issue:
clear all;
clc;
Struct.a.b.c.d = [1 2 3; 4 5 6]; % Structure containing the Matrix
structString = 'a.b.c.d(:,1)'; % String with the desired path
Ideally, I would like to access to the matrix with something like:
value = Struct.(structString)
But it doesn't work...
Thanks in advance for your help!

Accepted Answer

Walter Roberson
Walter Roberson on 10 Aug 2015
The function below only handles plain fields, no indexing. There could be some justification for extending it to handle indexing of the final element, but indexing anywhere else gets problematic, requiring more advanced checking
Sample:
Struct.a.b.c.d = [1 2 3; 4 5 6]; % Structure containing the Matrix
structString = 'a.b.c.d'
t = struct_stridx(Struct, structString);
t = t(:,1);
function v = struct_stridx(S, fieldlist)
fn = regexp(fieldlist, '\.', 'split');
bad_fields = fn(cellfun(@isempty,regexp(fn, '^[A-Za-z][A-Za-z0-9_]*$')));
if ~isempty(bad_fields)
error('Only plain fieldnames are allowed. First invalid one is "%s"', bad_fields{1});
end
v = S;
for K = 1 : length(fn)
thisfn = fn{K};
if isfield(v, thisfn)
if length(v) == 1
v = v.(thisfn);
else
error('MATLAB:dotRefOnNonScalar', 'Dot name reference on non-scalar structure. Field "%s"', strjoin(fn(1:K), '.'));
end
else
error('Field "%s" does not exist in structure', strjoin(fn(1:K), '.'));
end
end
end
  2 Comments
Rodolfo_H
Rodolfo_H on 10 Aug 2015
Hi, Thanks for your answer and the code you provided. I actually solved my problem by converting all the structure field into a string and evaluating that. The following lines show the application of the created function.
Struct.a.b.c.d = [1 2 3; 4 5 6]; % Structure containing the Matrix
structString = 'a.b.c.d(:,1)'; % String with the desired path
t = eval_struct_str(Struct, structString)
with:
function v = eval_struct_str( S , string_field_list )
v = eval(['S.' string_field_list]);
end
With that solution, the indexing is possible. Thanks again for your answer, my problem is solved.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 9 Aug 2015
Why are you trying to get a string involved in this at all? Why not just get the values directly from the structure:
value = Struct.a.b.c.d(:,1)
I just don't see any need to involve a string at all.
  1 Comment
Rodolfo_H
Rodolfo_H on 10 Aug 2015
Thanks for your answer.
The few lines of code that i written here are just supposed to represent the problem. In my "real" function, the structString variable comes from an Excel File:
[num structString] = xlsread(ExcelFile,'Sheet1','A1');
I actually solved my problem with the following line:
value = eval(['Struct.' structString{1}]);
Thanks again for your answer.

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!