Iterating through a structure
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Jordan Coombs
am 12 Dez. 2020
Kommentiert: Jordan Coombs
am 13 Dez. 2020
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 Kommentare
Ive J
am 13 Dez. 2020
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?
Akzeptierte Antwort
Rik
am 13 Dez. 2020
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);
Weitere Antworten (1)
Ive J
am 13 Dez. 2020
Bearbeitet: Ive J
am 13 Dez. 2020
Try this:
mask = arrayfun(@(x)inpolygon(X, Y, x.X, x.Y), no_afr_stru, 'UniformOutput', false);
2 Kommentare
Rik
am 13 Dez. 2020
I suspect the goal is to create a single mask, which would be easier with a loop. Also, arrayfun will have an overhead.
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!