Help saving the output of a for loop within a function into a vector.
Show older comments
Hello. I am currently writing the below function:
function [numout, outvec]= SCAN_DATA(vectin, lowlim, uplim)
%Compares each element of vectin against lowlim and uplim and returns
%a count (numout) of how many vector elements are greater than or less than the
%limits and a vector (outvec) of the actual out-of-limit readings
for k=1:length(vectin)
if vectin(k)>lowlim && vectin(k)<uplim;
outvec=vectin(k)
numout=numel(outvec)
else outvec=0
end
end
I would like to save the data output into the vector named vectin but I only get the last output run through the loop. I am pretty new to this after looking around online, I can't figure it out. Can anyone help?
Accepted Answer
More Answers (1)
Andrei Bobrov
on 24 Feb 2015
l0 = vectin>lowlim & vectin<uplim;
outvec = l0*vectin;
numout = nnz(l0);
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!