Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
finding content in structure

Subject: finding content in structure

From: Lin

Date: 20 Mar, 2009 16:16:01

Message: 1 of 4

Hi all..

Say i have a structure. When a column within the struct has more than 1 item, it is possible to list those index out?

%data
zz(1).k= {[1;0],[], [1;1];[2;2],[1;3],[] }
zz(2).k= {[1;0],[], [1;1];[2;2],[1;3],[2;2]; [1;3], [], []}

%expected results

column 1 of zz(1,1).k has 2 items [1;0] and [2,2]

column 1 of zz(1,2).k has 3 items [1;0] [2;2] and [1;3]
column 3 of zz(1,2).k has 2 items [1;1] and [2;2]

Thanks in advance.

Subject: finding content in structure

From: Bradley

Date: 20 Mar, 2009 18:27:01

Message: 2 of 4

This does basically what you asked for; you can add in an edit to include the specific items in the columns if you wish. Note some lines may be wrapped by the post format.

names = fieldnames(zz);
numfound = 0;
for n = 1:numel(names)
    for i = 1:numel(zz)
        for column = 1:size(zz(i).(names{n}),2)
            num_notempty = 0;
            for row = 1:size(zz(i).(names{n}),1)
                if(~isempty(zz(i).(names{n}){row,column}))
                    num_notempty = num_notempty + 1;
                end
            end
            if(num_notempty > 1)
                numfound = numfound + 1;
                report{numfound} = ['column ' num2str(column) ' of zz(' num2str(i) ').' names{n} ' has ' num2str(num_notempty) ' items'];
            end
        end
    end
end
disp(report)

Subject: finding content in structure

From: Lin

Date: 24 Mar, 2009 04:51:01

Message: 3 of 4

"Bradley" <starstrike99@yahoo.com.au> wrote in message <gq0n5l$gi4$1@fred.mathworks.com>...

It is possible to replace the nested for loop,

for column = 1:size(zz(i).(names{n}),2)
> num_notempty = 0;
> for row = 1:size(zz(i).(names{n}),1)
> if(~isempty(zz(i).(names{n}){row,column}))
> num_notempty = num_notempty + 1;
> end
> end
> if(num_notempty > 1)
> numfound = numfound + 1;
> report{numfound} = ['column ' num2str(column) ' of zz(' num2str(i) ').' names{n} ' has ' num2str(num_notempty) ' items'];
> end
> end
> end
> end

to something similiar i found in the FEX using cellfun

index1 = ~cellfun(@isempty,zz);
index2 = repmat(sum(index1)>1,[size(zz(i).(names{n}),2)]);
index3 = idx2 & idx1;
then list the row and column where the items are found.

I tried using structfun, but i got an error in the second statement. hence i am not able to proceed on. help anyone? Thanks in advance

Subject: finding content in structure

From: us

Date: 24 Mar, 2009 08:38:01

Message: 4 of 4

"Lin"
> Say i have a structure. When a column within the struct has more than 1 item, it is possible to list those index out

you may want to look at these two FEX submissions

http://www.mathworks.com/matlabcentral/fileexchange/1348

http://www.mathworks.com/matlabcentral/fileexchange/12278

us

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us