Unrecognized function or variable

10 次查看(过去 30 天)
toastgt
toastgt 2023-12-11
I'm having a problem where whenever I try to assign a function to a variable it says its an "Unrecognized function or variable" although its defined in my code. The code is below:
t = 1:1:2960;
t2= t/128;
t2 = t2';
%rc is raw data
%rcf is segmented data
fs = 128;
Lab5 = lowpass(realLab5.LEDC2,20,fs);
Lab5c= Lab5(116:2960);
m=1;
n=length(Lab5c);
for i = 2:n-1
if Lab5c(i)>Lab5c(i-1) && Lab5c(i)>=Lab5c(i+1) && Lab5c(i)> 488000
val(m) = Lab5c(i);
pos1(m) = i;
m = m+1;
end
end
ppg_peaks = m-1;
ppg_pos = pos1(m);
ppg_val = val;
The error is :
Unrecognized function or variable 'pos1'.
Error in Heart_Rate (line 22)
ppg_pos = pos1(m);
  1 个评论
Walter Roberson
Walter Roberson 2023-12-11
By the way, you might want to consider using findpeaks with the minimum peak prominence option

请先登录,再进行评论。

回答(2 个)

Voss
Voss 2023-12-11
编辑:Voss 2023-12-11
if Lab5c(i)>Lab5c(i-1) && Lab5c(i)>=Lab5c(i+1) && Lab5c(i)> 488000
If that condition is never true then the block inside will never be executed
val(m) = Lab5c(i);
pos1(m) = i;
m = m+1;
So pos1 and val will be undefined after the loop ends and m will still be 1.

Image Analyst
Image Analyst 2023-12-12
编辑:Image Analyst 2023-12-12
Try this:
t = 1:1:2960;
t2= t/128;
t2 = t2';
%rc is raw data
%rcf is segmented data
fs = 128;
Lab5 = lowpass(realLab5.LEDC2,20,fs);
Lab5c= Lab5(116:2960);
m=1;
n=length(Lab5c);
% Assign nans to pos1:
pos1 = nan(1, n);
for i = 2:n-1
if Lab5c(i)>Lab5c(i-1) && Lab5c(i)>=Lab5c(i+1) && Lab5c(i)> 488000
val(m) = Lab5c(i);
pos1(m) = i;
m = m+1;
end
end
% Alert user if no element of pos1 ever got assigned.
if all(isnan(pos1))
warningMessage = sprintf('No element of pos1 ever got assigned!!!')
uiwait(warndlg(warningMessage));
end
ppg_peaks = m-1;
ppg_pos = pos1(m);
ppg_val = val;
Set a breakpoint on the pos1(m)=1 line and see if it ever gets there (it won't). See this link:

类别

Help CenterFile Exchange 中查找有关 String 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by