Error using length, Too many input arguments.

조회 수: 2 (최근 30일)
C A
C A 2021년 8월 30일
댓글: Walter Roberson 2021년 9월 1일
I have a bunch of data from animal cells at different times. j represents the time frame. I want to extract a field from my object and that field can either have one or two values. If I have one value, I want to extract the value as it is and if I have two values, I want to average them out. This is a snippet of my code.
if length(Obj.MatFile{cellnumber}{j},ExtractField)==2
Answer(icellnumber,i)=mean(Obj.MatFile{cellnumber}{j}.(ExtractField));
else
Answer(icellnumber,i)=Obj.MatFile{cellnumber}{j}.(ExtractField);
end
I keep getting the above error. Any help would be greatly appreciated. Thanks in advance.

채택된 답변

Matt J
Matt J 2021년 8월 30일
편집: Matt J 2021년 8월 30일
The first line shouldn't have any commas.
  댓글 수: 3
Matt J
Matt J 2021년 8월 31일
Ina later line of the code, you have
Obj.MatFile{cellnumber}{j}.(ExtractField)
Shouldn't it be referenced that way everywhere else, too?
C A
C A 2021년 8월 31일
Thank you. This helps a lot!

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

추가 답변 (1개)

Stephen23
Stephen23 2021년 8월 31일
편집: Stephen23 2021년 8월 31일
if length(Obj.MatFile{cellnumber}{j}.(ExtractField))==2
But note that you do not need to check the length: taking the mean of one value simply returns that value, so in both cases you can simply do this (and not worry about how many values there are):
Answer(icellnumber,i) = mean(Obj.MatFile{cellnumber}{j}.(ExtractField));
and then get rid of the IF entirely.
  댓글 수: 2
C A
C A 2021년 8월 31일
Thank you. This is the most sensible way to do this. I just wanted to learn how to use 'length' function because I need to remove values if there are more than two.
Walter Roberson
Walter Roberson 2021년 9월 1일
"if there are more than two" -- use numel() instead of length() if you want to know how many values there are. length() has a slightly different meaning.

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

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by