Passing one element from array of structures

조회 수: 2 (최근 30일)
Neil dencklau
Neil dencklau 2013년 4월 2일
I have an array of structures, all with the same fields, and I am trying to pass them one at a time into another function.
What I am doing:
f1 = linspace(1,10,10)
f2 = linspace(11,20,10)
[F1,F2] = meshgrid(f1,f2)
s(numel(F1)).field1 = 0
for ii =1:numel(F1)
s(ii).field1 = F1(ii)
s(ii).field2 = F2(ii)
s(ii).field3 = 0
s(ii) = funct1(s(ii))
end
Where funct1 is my function that generates a value for s(ii).field3 based on the values of field1 and field2
I get the error:
Error using subsindex Function 'subsindex' is not defined for values of class 'struct'.
I have also tried: s = arrayfun(@(x) funct1(x), s) but got the same error
What am I doing wrong and how can I fix it?

답변 (1개)

Brian B
Brian B 2013년 4월 2일
What is in funct1? If I define
funct1 = @(s)s;
then your code works without error.
  댓글 수: 1
Brian B
Brian B 2013년 4월 2일
My guess is that somewhere you are trying to use a struct as the index, as in
F1(s(1))
which does indeed produce the error you have.

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

카테고리

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