Function keeps running into errors when I call it

1 次查看(过去 30 天)
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 个)

产品

Community Treasure Hunt

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

Start Hunting!

Translated by