Passing structure field name in function

조회 수: 3 (최근 30일)
I M
I M 2016년 6월 9일
편집: per isakson 2016년 6월 9일
I have a function that accepts a structure and a field_identifier (character format) as inputs. The structure loaded has substructures in it, for which the field_identifier loaded in the function matches. The format is the following: structure(i).substructure.field_identifier
I have two checkpoints, ~isstruct and ~isfield for each of the inputs and it works fine. After that I initialize a for loop, defined as:
i=1;
for i=1:size(structure(i).substructure.field_identifier,1)
With this, I get the error that field_identifier is not a valid field. I understand the problem lies in the way I am passing the field identifier, but I have tried several things and cannot figure a way of passing field_identifiers as arguments for the for loop within this function. Can someone tell me what am I doing wrong?
Thanks in advance for the advice!

채택된 답변

Stephen23
Stephen23 2016년 6월 9일
편집: Stephen23 2016년 6월 9일
structure(i).substructure.(field_identifier)
Note that it is recommended to avoid using i and j for loop variable names, because these are names of the inbuilt imaginary unit.
  댓글 수: 1
I M
I M 2016년 6월 9일
Thanks! I have implemented now a version with getfield, but this is more appropriate.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by