23 views (last 30 days)

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
on 21 Sep 2020

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'

Image Analyst
on 21 Sep 2020

Vasishta Bhargava
on 19 Sep 2020

Edited: Walter Roberson
on 21 Sep 2020

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)

Vasishta Bhargava
on 19 Sep 2020

Put the filepath as

K = csvread('...');

You have used a string " ..."

Image Analyst
on 19 Sep 2020

Vashista - that does not matter. It will work either with single or double quotes.

Image Analyst
on 19 Sep 2020

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)

Walter Roberson
on 21 Sep 2020

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) ?

Walter Roberson
on 21 Sep 2020

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.

Image Analyst
on 21 Sep 2020

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

Opportunities for recent engineering grads.

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/596422-unrecognized-function-or-variable-when-reading-an-excel-file#comment_1017979

⋮## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/596422-unrecognized-function-or-variable-when-reading-an-excel-file#comment_1017979

Sign in to comment.