Iterating through a structure

조회 수: 2 (최근 30일)
Jordan Coombs
Jordan Coombs 2020년 12월 12일
댓글: Jordan Coombs 2020년 12월 13일
I am trying to create a mask using by 168x1 struct but whenever i run the code i get this error - Expected one output from a curly brace or dot indexing expression, but there were 168 results.
Is there a way i can iterate through the structure and create a mask out of these fields.
Any help is appreciated
[x,y] = pixcenters(R);
[X,Y] = meshgrid(x,y);
names = fieldnames(S);
no_afr_stru = cell2struct(filt_nonaf_country,names,1);
fn = fieldnames(no_afr_stru);
rx = no_afr_stru.X(1:end-1);
ry = no_afr_stru.Y(1:end-1);
mask = inpolygon(X,Y,rx,ry);
  댓글 수: 2
Ive J
Ive J 2020년 12월 13일
Please provide more details so that everyone can reproduce your error. For starters, what are X, Y, rx and ry? can you provide an example?
Jordan Coombs
Jordan Coombs 2020년 12월 13일
X & Y are the points from my geotiff file and rx and ry are the points from the countries which i want to mask on my tiff file. as my struct no_afr_stru is 168x1 i want to iterate over all 168 lines in the structure and apply them all to the mask. Hope this helps!

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

채택된 답변

Rik
Rik 2020년 12월 13일
You can index a struct in a loop:
rx = no_afr_stru(n).X(1:end-1);
ry = no_afr_stru(n).Y(1:end-1);
  댓글 수: 1
Jordan Coombs
Jordan Coombs 2020년 12월 13일
Thank you that really helped!

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

추가 답변 (1개)

Ive J
Ive J 2020년 12월 13일
편집: Ive J 2020년 12월 13일
Try this:
mask = arrayfun(@(x)inpolygon(X, Y, x.X, x.Y), no_afr_stru, 'UniformOutput', false);
  댓글 수: 2
Rik
Rik 2020년 12월 13일
I suspect the goal is to create a single mask, which would be easier with a loop. Also, arrayfun will have an overhead.
Jordan Coombs
Jordan Coombs 2020년 12월 13일
you would be correct there sorry i should have specified

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

카테고리

Help CenterFile Exchange에서 Author Block Masks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by