Find function - how to use or index to a return when "Find" cannot find anything.

67 次查看(过去 30 天)
We have some sophisticated isotope hardware coupled to the most useless software imaginable. I have written some code to identify a series of retention times, fill out a new column of RT's for cross matching and also fill in the compound names for identification. Where chromatographic peaks are too close to separate by retention time alone I have added the facility to look for the largest peak size or first or last on a doublet.
But I want the code to show 'not found' values if the FIND function can't find a value at all. Sometimes a peak is missing. This way, all my data files will be the same length.
In the portion of code here, it all works fine until you change the first IF statement to elseif and insert the new IF statement above it that seeks to identify when FIND doesn't find. I have tried a few things including ISEMPTY in the first IF statement. But while this works fine in the command window, coupled inside the IF statement it won't work.
I need some way to identify a missing value for RTidx. Anyone?
start = 1;
finish = loc(1);
R = rt(start:finish);
for C = [1:10, 13, 14, 15, 17:18, 20:22];
RTidx = find(R >= S(C) & R <= E(C));
if RTidx = [ ];
name(RTidx) = not_found(C);
elseif (length(RTidx)>1);
[junk, i] = max(mv(RTidx));
rt_fill(RTidx(i)) = rt(RTidx(i));
name(RTidx(i)) = amino_list(C);
else
rt_fill(RTidx) = rt(RTidx);
name(RTidx) = amino_list(C);
end;
RT_FILL(LISTidx) = rt_fill;
NAME_FILL(LISTidx) = name;
end
  1 个评论
bym
bym 2011-10-26
I don't know if it makes a difference, but it looks like you have [ ](a space in the empty matrix). can you try [] (no space)

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2011-10-26
Your line
if RTidx = [ ];
would attempt to assign the empty array to RTidx, which is not something you can do in an "if" statement.
if isempty(RTidx)
is the appropriate test.
You might want to cross-check with size(RTidx) and class(RTidx) to see what is happening.
  1 个评论
Rhys Leeming
Rhys Leeming 2011-10-27
As a beginner, the help browser does not make it clear (or give examples) that the ISEMPTY function can be just a query without the need for an = sign.
ISEMPTY has worked fine. Thanks!

请先登录,再进行评论。

更多回答(1 个)

the cyclist
the cyclist 2011-10-26
I would think the best way to do this would be with the isempty() command, but you don't show what you tried there.
However, I also notice that you have used
if RTidx = []
rather than
if RTidx == []
You need the doubled equal sign for checking equality. A single equal sign is for assignment.
  1 个评论
Rhys Leeming
Rhys Leeming 2011-10-27
As a beginner, the help browser does not make it clear (or give examples) that the ISEMPTY function can be just a query without the need for an = sign.
Trying to query the index with RTidx == [] didn't work. Thanks anyway.
ISEMPTY has worked.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by