How to create a matlab function?
1 回表示 (過去 30 日間)
古いコメントを表示
Hi, I'm trying to create my own matlab function, but I don't really understand the way it works;
Here is what I have:
function [Y] = CalibratieABP(ABP)
%UNTITLED5 Summary of this function goes here
% Detailed explanation goes here
afstand = 4; %over hoe lang je het gemiddelde neemt
k = 4; %onder en bovengrens
lengte=25; %lengte hoelang achter elkaar +- 0
%%ABP
p = ABP; %ABP of CVP? ook aanpassen in titel
% gemiddelde stukje nemen
mean = movmean(p,afstand);
Y1 = mean<k;
Y2 = mean>-k;
Yy = Y1.*Y2;
%minimaal aantal opeenvolgende 1
Y = zeros(size(p));
locations = strfind([Yy'], ones(1,lengte));
for i=1:lengte
Y([locations+i-1]) = 1;
end
end
the %adds some extra information.
I know that i'm doing something something wrong with the function at the beginning, but I don't see what it should be like.
I want ABP as input and Y as output.
Thanks!
0 件のコメント
回答 (1 件)
Wilson A N
2017 年 1 月 16 日
The format in your script is correct for ABP as input and Y as output. Also, when creating your own MATLAB function you need to save the function .m file with the same name as the function. In your case the function .m file should be named as 'CalibratieABP.m'. For more information just check the documentation given below: https://www.mathworks.com/help/matlab/ref/function.html
I tried the above script and called the MATLAB function as
>> a = Calibratie(10);
from the MATLAB command line. This is working for me.
2 件のコメント
Guillaume
2017 年 1 月 16 日
Yes, it's not clear what Tessa is having as the function works fine as it is.
The code could do with some improvements though. For a start what is the point of renaming ABP into p. Just use the variable ABP in the code. Calling a variable mean is not a good idea since it shadows the mean function. And while .* works for computing the AND of two logical vectors, & is a lot clearer. In any case, testing the bounds of the mean can all be done in one line. Finally, if you have R2016b, you can use implicit expansion to remove the loop. So:
function [Y] = CalibratieABP(ABP)
%UNTITLED5 Summary of this function goes here
%Detailed explanation goes here
validateattributes(ABP, {'numeric'}, {'column'}); %assumptions that were built into the original code
afstand = 4; %over hoe lang je het gemiddelde neemt
k = 4; %onder en bovengrens
lengte=25; %lengte hoelang achter elkaar +- 0
p = ABP; %ABP of CVP? ook aanpassen in titel
% gemiddelde stukje nemen
haslowmean = abs(movmean(ABP, afstand)) < k;
locations = strfind(haslowmean.', ones(1,lengte));
Y = zeros(size(ABP))
Y([locations+(0:lengte-1)']) = 1;
end
Image Analyst
2017 年 1 月 16 日
Maybe she's not calling it and passing it data. Maybe she's just hitting F5 or clicking the green run triangle. Who knows? Only Tessa, because she didn't tell us what the problem was, like pasting ALL the red text from an error message, or giving us the output data and saying why it's not right, or something.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!