필터 지우기
필터 지우기

check if numbers exist in a structure field

조회 수: 14 (최근 30일)
Katherine
Katherine 2014년 1월 10일
댓글: Image Analyst 2014년 1월 10일
I have a set of values in a vector that I would like to loop over to check whether they exist in a structure field--is there any easy way to do this without causing an index error. I want to check whether all of the values in j,
where j=[1:79 356:366], exist in my structure files(1,1).day(j).median.
I was trying to loop over the elements of j and set restrictions via if then statements. The cases I would like to test but am not sure how to test are:
-whether the values exist in the structure field. If they exist I would like to calculate a value
-whether they are empty (no data) I would like to skip them and continue looping through the rest of the values of j
-whether they exist in the structure field at all--skip and continue looping through the rest of the values of j
-If 80% of the numbers exist I would like to calculate something as well.
I've tried a slew of things but keep getting the indexing error. Can anyone point me to how I would set up this logic?
  댓글 수: 2
Jos (10584)
Jos (10584) 2014년 1월 10일
So, you have a situation like this
files.day(1).median = 1
files.day(3).median = 3
files.day(4).median = 3
with files.day(2).median being therefore empty, and
j = [1 2 4]
A = [files.day(j).median]
produces the wrong result in A? because you want to be something like [1 NaN 4] ?
Katherine
Katherine 2014년 1월 10일
I have a situation where for this current data set files(i,1).day(k) has 335 days in it. I want to make a versatile code so that k could be any number of days. I essentially want to test whether the day values in j exist in the actual dataset because I am trying to compile seasonal calculations but only based on the actual dataset that I have. So for j = [1:79 356:366] I am trying to test whether the dataset has julian days for the winter, and if they exist I want to pull out the ones that actually exist and do a calculation, if they are empty I want to skip them, and if they don't exist I would like to skip them too.

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

답변 (1개)

Simon
Simon 2014년 1월 10일
Hi!
You may write
m = [files(1,1).day(:).median];
to get a vector of all values in 'day'. Then you can use ismember/intersect/setdiff/... to test which values exist.
  댓글 수: 3
Matt J
Matt J 2014년 1월 10일
편집: Matt J 2014년 1월 10일
This won't work if some
files.day(k).median=[]
are empty. A modification of Simon's technique that would work is
m={files(1,1).day(j).median}; %fill cell array
empty_days = j(cellfun('isempty',m))
Image Analyst
Image Analyst 2014년 1월 10일
You can use fieldnames to determine if a member of a structure, such as day or median above, actually exist.
fieldnames(files)
fieldnames(files.day)
fieldnames(files.day.median)

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by