Ejay - see Scripts vs Functions to understand the differences between scripts (which you have written) and function (which you want to write). Since the first line of code (in the above) is neither a comment nor a function signature, then MATLAB assumes that you have written a script. You need to change your code so that the first line is the signature function zc_idx = ZeroCrossing(t,emg6)
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0);
zx = zci(emg6);
zc_idx = zeros(numel(zx),1);
for i = 1:numel(zx)
idx = max([1 zx(i)-1]):min([zx(i)+1 numel(emg6)]);
x_range = t(idx);
y_range = emg6(idx);
zc_idx(i) = interp1( y_range(:), x_range(:), 0, 'linear', 'extrap' );
end
end
The code
Fs=200;
samples=0:2186;
t = samples/Fs;
t(:,[1]) = [];
should be used either in the body of the function or outside of the function (where you need to define the input parameter t).
Best Answer