How to run checks on all rows of structure?

2 views (last 30 days)
I want to check all the rows in the field of a structure and find out which rows are not empty. Can I get an index without using a loop?
For example, with the following structure:
s.a=rand(5);
s(2,1).a=[];
s(3,1).a=rand(5);
s(4,1).a=[];
I would be hoping for something like:
idx=[1 0 1 0].';
My current code is:
idx = zeros(size(s,1),1);
for c = 1:size(s,1)
if ~isempty(s(c).a)
idx(c)=1;
end
end
EDIT: subscripts for structure with rows as opposed to columns.

Accepted Answer

Stephen23
Stephen23 on 16 Dec 2019
Edited: Stephen23 on 16 Dec 2019
"How to run checks on all rows of structure?"
Your structure only has one row (and four columns):
>> size(s)
ans =
1 4
If you want to check the size of a particular field of a non-scalar structure, then try something like this:
>> F = @(q)isempty(q.a);
>> X = ~arrayfun(F,s)
X =
1 0 1 0
  1 Comment
Turlough Hughes
Turlough Hughes on 16 Dec 2019
Thanks. It works on rows as well. The sample I gave above was a mistake.

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!