Function keeps running into errors when I call it

조회 수: 1 (최근 30일)
Kristin Aldridge
Kristin Aldridge 2021년 10월 17일
댓글: Star Strider 2021년 10월 17일
Hello,
I am running into errors when I try and call my function below by using
[phivalues] = findphases(wpeakt,hpeakt)
%Function below
function [phivalues] = findphases(wpeakt,hpeakt)
newhpeaks = [ ];
phivalues = [ ];
hpeakt = sorteddata.data(:,1);
wpeakt = sorteddata.data(:,5);
for i=1:length(wpeakt)-1;
tau=wpeakt(i+1)-wpeakt(1);
newhpeaks = hpeakt(find(wpeakt(i)<hpeakt<wpeakt(i+1))); %finds haltere value between Tau wing peaks
newhpeaks = newhpeaks(1); %finds "first" neural spike
deltat=newhpeaks-wpeakt(i);
phi=2*pi*(deltat/tau);
phivalues = [phivalues phi];
end
plot(wpeakt,ones(size(wpeakt)),'bo');% ones is y axis
hold on
plot(hpeakt,ones(size(hpeakt)),'ro');
end
The data is being imported as a 1x1 struct, which is why hpeakt and wpeakt are sorteddata.data. hpeakt is all of column 1, and wpeakt is all of column 5. When I call the function I get the error "Unrecognized function or variable 'wpeakt'." When I highlight the entire function script without the function line it comes up fine with my graph and everything. I'm not sure why I am running into issues calling it though. I have my function file named "findphases."
I am also getting the error
>> [phivalues] = findphases(wpeakt,hpeakt)
Unable to resolve the name sorteddata.data.
Error in findphases (line 5)
hpeakt = sorteddata.data(:,1);
Any help is appreciated.

채택된 답변

Star Strider
Star Strider 2021년 10월 17일
Try this instead —
hpeakt = sorteddata.data(:,1);
wpeakt = sorteddata.data(:,5);
[phivalues] = findphases(wpeakt,hpeakt)
function [phivalues] = findphases(wpeakt,hpeakt)
newhpeaks = [ ];
phivalues = [ ];
for i=1:length(wpeakt)-1;
tau=wpeakt(i+1)-wpeakt(1);
newhpeaks = hpeakt(find(wpeakt(i)<hpeakt<wpeakt(i+1))); %finds haltere value between Tau wing peaks
newhpeaks = newhpeaks(1); %finds "first" neural spike
deltat=newhpeaks-wpeakt(i);
phi=2*pi*(deltat/tau);
phivalues = [phivalues phi];
end
plot(wpeakt,ones(size(wpeakt)),'bo');% ones is y axis
hold on
plot(hpeakt,ones(size(hpeakt)),'ro');
end
I obviously cannot test this, so I leave that to you.
Define the variables first, then pass them to the function.
Also, the first plot will be a horizontal line at 1 going from ‘min(wpeakt)’ to ‘max(wpeakt)’. The second will be similar. Is that what you want?
.
  댓글 수: 2
Kristin Aldridge
Kristin Aldridge 2021년 10월 17일
It worked! Thank you. Yes the plot is a horizontal line because the peaks are "in phase" so that is correct.
Star Strider
Star Strider 2021년 10월 17일
As always, my pleasure!
(I just wanted to be sure about the plots.)
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by