Filling iteration with NaN when output argument is not assigned a value in execution of function.

1 view (last 30 days)
I have a plot with acceleration data vs time (in frames). There are certain regions of this plot that have features of interest like threshold crossings. To find the threshold crossings I have found the endpoints of a possible feature appearance and created several ranges of values, both acceleration and time, for which to search for the presence of a threshold crossing and if present document the time it occurs at. The issue arises when there is a range that doesn't have a crossing at the specified threshold (25). The result is the error "Output argument "xv" not assigned a value in the execution with FindLevel function". I understand it is giving me this error because it can't find a value in the range, however, instead of stopping the loop I want it to assign a NaN value instead and then move onto the next iteration. I tried making a conditional statement where it looks at an array containing the minimum value of each range and then if the minimum value exceeds the threshold it is assigned the value NaN, but I haven't been able to apply/connect this to the threshold finding part of the loop.
for i=1:size(CoMStartRangeAcc,1)
if (MinArray(i,:)>25)%the idea here is if the min acceleration value is below the threshold it isn't crossed and should be filled with an NAN
final(i,:)=NaN
else
FrameTimeComstart=FindLevel(CoMStartRangeFrame(i,:),CoMStartRangeAcc(i,:),25)
RowMakerComstart=FrameTimeComstart(1,1).';
final(i,:)=RowMakerComstart;
end
end
function xv = FindLevel(x,y,level) %the function I am using to find the threshold crossing times
cxi = find(diff(sign(y-level)));
for k = 1:numel(cxi)
idxrng = max(1,cxi(k)-1) : min(numel(x), cxi(k)+1);
xv(k) = interp1(y(idxrng), x(idxrng), level);
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 25 Aug 2022
Inside the function, initialize
xv = nan;
Then if you do not assign anything else inside the loop, xv will still have been assigned a value.
  1 Comment
T-800
T-800 on 25 Aug 2022
Edited: T-800 on 25 Aug 2022
So simple and it works! Thanks!
Just for visibility this is Roberson's edit:
function xv = FindLevel(x,y,level)
cxi = find(diff(sign(y-level)));
xv = nan;
for k = 1:numel(cxi)
idxrng = max(1,cxi(k)-1) : min(numel(x), cxi(k)+1);
xv(k) = interp1(y(idxrng), x(idxrng), level);
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!