Unrecognized function or variable when reading an excel file.

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)

 Accepted Answer

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 Comments

Thank you!! It's very clear for me now. I didn't expect all the values to be above 90.
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.

Sign in to comment.

More Answers (3)

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 Comments

That isn't seem to be the problem. I'm getting the same error. This code runs and gives the required output when running on other devices. Doesent work on mine though. I'm new to using Matlab so am I missing any tool box or something?
ps-Didn't make a difference when entering like this. (last>=0)
Didn't get the error when entering p=0 before if clause as stated in the other answer.
It doesn't run for us. Why not? Because you keep forgetting to attach your data.
Put the filepath as
K = csvread('...');
You have used a string " ..."
Vashista - that does not matter. It will work either with single or double quotes.

Sign in to comment.

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 Comments

This was my answer to him ... But he said that's not the problem . It works on other devices
Sorry, you did say that. But either he's wrong or he's not telling us the whole story. SOMETHING changed if it's able to run on different computers, he just forgot to tell us what. Maybe it depends on the data or something.
Got the attached on two different divices when runing the code. I thought this graph should be the correct graph. That's why I said it worked before. I'm also trying to figure what changed, I didn't forget. Maybe those divices didn't have the required toolboxes?
It's not a toolbox issue if it said that p was not defined. That's just a simple logic/programming error. I really doubt that that same program with same data would work on one computer but not another. Anyway, it works now. Does it work for you? Does it give you the expected and desired answer?
for f = 1:length(e)
if (e>0)
if (e<80)
disp("normal heart beat rate")
elseif (e>90)
disp("critical condition")
else
disp ("Stressed,the person needs to rest")
end
end
end
I want to display these according to the heart rates.
Not sure what e is, but okay. Is there any problem, or even a question? If so, what?
Sorry it's not e it's y,
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
I was trying to display those messeges considering the BPM values according to the conditions. What am I missing? What should I do to get the messeges displaying according to the conditions?
Did you use the debugger to step through your code and find out why it's not getting to the disp() commands? If not, why not?
I was expecting a final graph like the one attached.
The attached is when evaluating this part of the code
K = csvread("F:\DIP SEM 2\DIGITAL\Project B2\HeartBeatSample.csv") %replace this with your path to the sample file.(using windows run/create a shortcut of csv and getting the path/typing manually)
Time = K(1:45465,1);
Voltage= K(1:45465,2); %/amplitude
plot(Time,Voltage) % Plotting the virtual data thats given
J = fir1(1000,1/1000*2,'high'); % Smoothing using high pass filter
Voltage_filter = filter(J,1,Voltage);
plot(Voltage_filter)
But when trying to diplay the messeges according to the conditions, the graph changes and doesen't display the messeges.
Sorry if I don't make any sense. I'm new to this.
if (y>0)
if (y<80)
disp("You're in Normal conditon")
Okay, to get to this disp(), y > 0 and y < 80
elseif (y>90)
disp("You're in Stressed condition")
And to get to this disp(), y > 0 and y >= 80 and y > 90, which can be simplified to y > 90.
else
disp ("You're in Critical condition")
And to get to this disp(), y > 0 and y >= 80 and y <= 90, which can be simplified to y >= 80 and y <= 90
... Are you sure that when you pass through "Normal" to "Stressed", that you are temporarily in Critical condition, but that clears up as your heart beats faster??
I was truing to get a warning variable which shows output (heart rate range) of normal, stressed and critical for the BPM of less than 80, between 80 and 90 and above 90 respectively.
Then you want your elseif (y>90) to be elseif (y<=90)
I'm trying to get these messeges displayed but they don't get displayed.

Sign in to comment.

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 Comments

Thank you!!.Now I understand why the written condition does not give the required display output. I would like some help on this to get display messages as the BPM values changs.
"only be testing the "current" value of y -- the one indexed by y(L)" I'm not sure how I can get the conditions to read the current values. When I try I always get 'You're in stress condition'.
I would appreciate your help.
for L = 1:length(y)
if (y(L)>0)
if (y(L)<80)
disp("You're in Normal conditon")
elseif (y(L)<=90)
disp("You're in Stressed condition")
else
disp ("You're in Critical condition")
end
end
end
However, you are just going to get a stream of those, one per point in your graph. You should consider displaying the time as part of the message. You should also consider only displaying the message when you change state.
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
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?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!