Unrecognized function or variable when reading an excel file.

1 次查看(过去 30 天)
This code is for monitoring BPM. Code works fine on another device without doing any changes. Can't figure why this error occures on this Matlab.
K = csvread("F:\DIP SEM 2\DIGITAL\Project B2\HeartBeatSample.csv") %path to the sample file
Time = K(1:45465,1);
Voltage= K(1:45465,2); %/amplitude
plot(Time,Voltage) % Plotting the virtual data that given
J = fir1(1000,1/1000*2,'high'); % Smoothing using high pass filter
Voltage_filter = filter(J,1,Voltage);
plot(Voltage_filter)
Findsq = Voltage_filter.^2;
plot(Findsq);
last = 0;
Point = 0;
pulse = zeros(length(Findsq),1);
for L = 1:length(Findsq)
if (Findsq(L) > 0.8)
if (Point == 0)
if (last > 0)
t = L - last;
p = 1000/t*60;
end
last = L;
end
Point = 100;
else
if (Point > 0)
Point = Point - 1;
end
end
pulse(L)=p;
end
plot(pulse); % plotting the pulse graph
%xlabel('Numbers Of Data');
ylabel('BPM')
title('BPM Graph');
DATA = findobj(gca,'Type','line')
y=get(DATA,'Ydata')
for L = 1:length(y)
if (y>0)
if (y<80)
disp("You're in Normal conditon")
elseif (y>90)
disp("You're in Stressed condition")
else
disp ("You're in Critical condition")
end
end
end
Error messege;
Unrecognized function or variable 'p'.
Error in Scenario2_groupB2 (line 35)
pulse(L)=p;
MATLAB Version 9.8 (R2020a)
Simulink Version 10.1 (R2020a)
Control System Toolbox Version 10.8 (R2020a)
DSP System Toolbox Version 9.10 (R2020a)
Data Acquisition Toolbox Version 4.1 (R2020a)
Signal Processing Toolbox Version 8.4 (R2020a)
Statistics and Machine Learning Toolbox Version 11.7 (R2020a)
Symbolic Math Toolbox Version 8.5 (R2020a)

采纳的回答

Image Analyst
Image Analyst 2020-9-21
Salinda: You can see from my code that your min pulse is 90.09 and the max is 138.2.
That is why it always says critical.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
K = csvread("HeartBeatSample.csv") %path to the sample file
Time = K(1:45465,1);
Voltage= K(1:45465,2); %/amplitude
subplot(2, 2, 1);
plot(Time,Voltage) % Plotting the virtual data that given
title('Voltage vs. Time', 'FontSize', fontSize, 'Interpreter', 'none');
J = fir1(1000,1/1000*2,'high'); % Smoothing using high pass filter
Voltage_filter = filter(J,1,Voltage);
subplot(2, 2, 2);
plot(Voltage_filter)
grid on;
title('Voltage_filter', 'FontSize', fontSize, 'Interpreter', 'none');
Findsq = Voltage_filter.^2;
subplot(2, 2, 3);
plot(Findsq);
grid on;
title('Findsq', 'FontSize', fontSize, 'Interpreter', 'none');
last = 0;
Point = 0;
pulse = zeros(length(Findsq),1);
for L = 1:length(Findsq)
p = 0;
if (Findsq(L) > 0.8)
if (Point == 0)
if (last > 0)
t = L - last;
p = 1000/t*60;
end
last = L;
end
Point = 100;
else
if (Point > 0)
Point = Point - 1;
end
end
pulse(L)=p;
end
subplot(2, 2, 4);
plot(pulse); % plotting the pulse graph
grid on;
title('Pulse', 'FontSize', fontSize, 'Interpreter', 'none');
%xlabel('Numbers Of Data');
ylabel('BPM')
title('BPM Graph');
y = pulse;
x = 1 : length(y);
% There are a large number of zeros. Let's set those to nan to ignore them
badIndexes = y <= 0;
% Get rid of those.
x(badIndexes) = [];
y(badIndexes) = [];
% Plot them in red.
hold on;
plot(x, y, 'r-', 'LineWidth', 2);
grid on;
% Find the min and put a line there.
darkGreen = [0, 0.7, 0];
yline(min(y), 'Color', darkGreen, 'LineWidth', 2);
caption = sprintf('Pulse. Min = %f, max = %f', min(y), max(y));
title(caption, 'FontSize', fontSize, 'Interpreter', 'none');
yPrior = rand(1); % Initialize
for L = 1 : length(y)
if ~isnan(y(L)) && y(L) ~= yPrior
if y(L) < 80
fprintf("You're in Normal condition at L = %d with a pulse of %f.\n", x(L), y(L));
elseif y(L) <= 90
fprintf("You're in Stressed condition at L = %d with a pulse of %f.\n", x(L), y(L));
else
fprintf("You're in Critical condition at L = %d with a pulse of %f.\n", x(L), y(L));
end
end
yPrior = y(L);
end
g = gcf;
g.WindowState = 'maximized'
  2 个评论
Salinda Nandasena Talapitiya Rallage
Thank you!! It's very clear for me now. I didn't expect all the values to be above 90.
Image Analyst
Image Analyst 2020-9-21
Are you sure what you call pulse is really the pulse rate? It looks more like the voltage waveform, and to get the pulse rate you'd have to get the distance between peaks.

请先登录,再进行评论。

更多回答(3 个)

VBBV
VBBV 2020-9-19
编辑:Walter Roberson 2020-9-21
You have initialized last = 0; but you used a if condition as
if (last>0)
...
p = 100*t/60; %
end
Which Is not satisfied at beginning Later at the end of for loop you are assigning
pulse(L) = p; % p is not initialized / defined before anywhere except in the if condition.
Change if condition as
if (last>=0)
  5 个评论
VBBV
VBBV 2020-9-19
编辑:Walter Roberson 2020-9-21
Put the filepath as
K = csvread('...');
You have used a string " ..."
Image Analyst
Image Analyst 2020-9-19
Vashista - that does not matter. It will work either with single or double quotes.

请先登录,再进行评论。


Image Analyst
Image Analyst 2020-9-19
You need to initialize p because on the first time, you reference p but it never got inside the if block to be assigned:
for L = 1:length(Findsq)
p = 0;
if (Findsq(L) > 0.8)
  14 个评论

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2020-9-21
When you have if expression then MATLAB considers the condition to be true only if all values of the expression are non-zero.
You are testing if y < 80 (for example) . y is a vector. Thus the test will only succeed if all y values are < 80. If for example all but one of them were less than 80 but the last one of them was 81, then it would not be the case that all of them were true (non-zero) and MATLAB would consider the condition to fail.
This also applies to your initial y > 0 test: if there is even one y value that is <= 0 then your entire outer if would be considered to fail and the inner test would not be done.
for L = 1:length(y)
As you are taking length(y) the implication is that you expect y to be a non-scalar at that point, probably a vector. You then loop with L taking on successive values from 1 to the number of entries in y.
Then inside that for L loop, you always do exactly the same thing for every different L value, and what you do does not depend upon the value of L. Every time, you are testing all of y in your statements.
Have you considered the possibility that inside your loop you should only be testing the "current" value of y -- the one indexed by y(L) ?
  4 个评论
Image Analyst
Image Analyst 2020-9-21
Perhaps
yPrior = rand(1); % Initialize
for L = 1 : length(y)
if y(L) > 0 && y(L) ~= yPrior
if y(L) < 80
disp("You're in Normal condition.")
elseif y(L) <= 90
disp("You're in Stressed condition.")
else
disp("You're in Critical condition.")
end
end
yPrior = y(L);
end
Salinda Nandasena Talapitiya Rallage
I tried Mr Walters way before. Then I only got "You're in stressed condition."
Same when I try this;
yPrior = rand(1); % Initialize
for L = 1 : length(y)
if y(L) > 0 && y(L) ~= yPrior
if y(L) < 80
disp("You're in Normal condition.")
elseif y(L) <= 90
disp("You're in Stressed condition.")
else
disp("You're in Critical condition.")
end
end
yPrior = y(L);
end
If the condition is correct then the only explanation is that the graph I'm receiving is faulty. Since every BPM couldn't be above 90 everytime.
My intention is to read the heartbeat samples given, plot that in a BPM graph and display the messeges according to the conditions. What am I missing here? could it be im using a wrong filter?

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by