How can I read data from excel file consecutively rather than taking random numbers ?

1 view (last 30 days)
clc
clear
%Adjust the normal graph values to prevent unnecessary overlaps.
RedMax=3.3402;
RedMin=0.9352;
%Adjust the normal graph values to prevent unnecessary overlaps.
BlueMax=0.9351;
BlueMin=0.6865;
%Maximum Background value for sensor 1.
BGMaxSensor1=0.6864;
%Minimun Background value for sensor 1.
BGMinSensor1=0.6007;
%Sensor 2 values
BlackMax=1.350; %This detects the signals in the background range, so consider this as the maximum value for blackline and anythig higher will be considered white because white color reflects greater than black color.
BlackMin=0.5629; %Since black color reflects the least amount of light this value for blackline is the minimum value in the considered range.
BGMaxSensor2=2.2075; %Consider this as the maximum value for sensor 2 because this is the peak value for refletion of white color.
BGMinSensor2=1.351; %Consider this as the minimum value for sensor 2 because anything lower will be in the black color range.
puck=0; %Searching for the Red puck.
sig1=0; %Signal value.
blackline=0; %Searching for the Black line.
while puck == 0
f=randi(3); %In order to access the file, randomly generates an integer.
r=randi(2000); %In order to access the row, randomly generates an integer.
%Enter your own path for the following 3 Excel sheets on your PC.
switch f
case 1%Name of the file is "RedLedOnRedPuck_01"
L=xlsread("F:\test\RedLedOnRedPuck_01.xlsx");
case 2 %Name of the file is "RedLedOnBluePuck_01"
L=xlsread("F:\test\RedLedOnBluePuck_01.xlsx");
case 3 %Name of the file is "RedLedOnRedBackground_01"
L=xlsread("F:\test\RedLedOnBackground_01.xlsx");
end
sig1=L(r,2); %Generate a random signal for varying conditions on ground.
clear L; %Since this is a while loop, this is to reset L path from workspace after every random signal generated.
if(sig1>=RedMin && sig1<=RedMax) %Make sure the random signal is within the described ranges.
color='red';
fprintf('Value of f is %g \n',f);
fprintf('Value of s1 is %f \n',sig1);
fprintf('Red Puck has been detected. \n');
puck=1; %A red puck is detected.
elseif(sig1>=BlueMin && sig1<=BlueMax) %Make sure the random signal is within the described ranges.
color='blue';
fprintf('Value of f is %g \n',f);
fprintf('Value of s1 is %f \n',sig1);
fprintf('Blue Puck has been detected. \n');
puck=0; %A red puck is not detected.
elseif(sig1>= BGMinSensor1&& sig1<=BGMaxSensor1) %Make sure the random signal is within the described ranges.
color='White';
fprintf('Value of f is %g \n',f);
fprintf('Value of s1 is %f \n',sig1);
fprintf('White Background has been detected. \n');
puck=0; %A red puck is not detected.
end
end
if(puck==1) %After detecting a red puck, the search for the blackline will begin.
while blackline==0
%Enter your own path for the following Excel sheet on your PC.
r=randi(2000); %In order to access the row, randomly generates an integer.
L=xlsread("F:\test\BlueLedOnBlackLineAndBackground_01.xlsx"); %The file here is "BlueLedOnBlackLineAndBackground_01".
sig1=L(r,2); %Generate a random signal for varying conditions on ground.
clear L; %Since this is a while loop, this is to reset L path from workspace after every random signal generated.
if(sig1>=BlackMin && sig1<=BlackMax) %Make sure the random signal is within the described ranges.
color='black';
fprintf('Value of f is %g \n',f);
fprintf('Value of s1 is %f \n',sig1);
fprintf('Black Line has been detected. \n');
blackline=1; %The blackline is detected.
elseif(sig1>= BGMinSensor2&& sig1<=BGMaxSensor2) %Make sure the random signal is within the described ranges.
color='White';
fprintf('Value of f is %g \n',f);
fprintf('Value of s1 is %f \n',sig1);
fprintf('No Black Line has been detected. \n');
blackline=0; %The blackline is not detected.
end
end
end
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)
  4 Comments

Sign in to comment.

Accepted Answer

Salinda Nandasena Talapitiya Rallage
I've managed to correct my code. Thank you.

More Answers (1)

Cris LaPierre
Cris LaPierre on 26 Sep 2020
Maybe I don't understand your question, or you don't understand your code.
Your code tells it to load a random file, and then chooses a random line to look at.
f=randi(3); %In order to access the file, randomly generates an integer.
r=randi(2000); %In order to access the row, randomly generates an integer.
You could use readtable to load the data into a table, and then loop through the table row by row.
% UNTESTED
L=readtable("F:\test\RedLedOnRedPuck_01.xlsx");
for r = 1:height(L)
sig1 = L{r,2};
end

Community Treasure Hunt

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

Start Hunting!