Why does an 'catch me' error pop up while im running my while loop

I am trying to compare values in an excel file but i do not know the where the last row of values in the exel file is at so i tried to compare them till the last cell is empty then i stop. But apparently when the code stops when the values has a sudden huge gap difference say from 7 to 11. I want the code to ignore if they cant compare the values and just put a '-' in the cell and continue running the comparison.
b=1;
if ~isempty(num(b,5))
if ismembertol(num(b,5),num(frame,9),0.001)
W = num(b,4);
ChainplusF = Originalchainage + W;
num(frame,8) = ChainplusF;
else
while ~ismembertol(num(b,5),num(frame,9),0.001) && ~isempty(num(b,5))
b=b+1;
if ismembertol(num(b,5),num(frame,9),0.001)
W = num(b,4);
ChainplusF = Originalchainage + W;
num(frame,8) = ChainplusF;
end
end
end
else
num(frame,8) = '-';
end

16 Comments

num appears to be numeric, such as the first output of xlsread(). It is possible to assign a single character such as '-' to such a location but the effect would be to store double('-') which would be 45.0
If you want to store the character '-' you are going to need to start using cell arrays.
however i have set breakpoint at the 'while' loop and at the 'else' but when comparing the last cell (at line 8886), in the 'while loop' it crashes without even entering the 'else'. I want it to enter the 'else loop' to just input something and continue looping the all over again.
These are the example in the array, hope it helps.
1.PNG
2.PNG
Can you attach the workbook with the paper clip icon?
This is the excel file which i am trying to write the values into. Columm 7,8 and 9 are written and compared by the code.
We are missing the controlling code that is setting frame.
We are missing the controlling code that is creating num . We might suspect that it is intended to be the data from the csv file but the code needs at least 9 columns of data, but the csv file only has 5 columns of data.
This is the full loop which i am trying to run: The num is created by xlsread(), and the other columns which are empty is because the code will write them into the excel file. The previous loop i showed was for comparing the values but somehow an error happend when the values that was being compared to skipped from 7... to 11... This was the error: Index exceeds matrix dimensions. Error in Synchronized (line 91)
if ismembertol(num(b,5),num(frame,9),0.001)
91 if ismembertol(num(b,5),num(frame,9),0.001)
for frame = 1 : numberOfFrames %!
% Extract the frame from the movie structure.
thisFrame = read(videoObject, frame);
% Display it
hImage = subplot(2, 2, 1);
image(thisFrame);
caption = sprintf('Frame %4d of %d.', frame, numberOfFrames); %!
title(caption, 'FontSize', fontSize);
drawnow; % Force it to refresh the window.
%********************************EXCEL MODIFICATION + Looping for comparison*************************
num(frame,7) = frame;
num(frame,9) = num(1,5) + j;
j= j+0.033;
b=1;
if ~isempty(num(b,5))
if ismembertol(num(b,5),num(frame,9),0.001)
W = num(b,4);
ChainplusF = Originalchainage + W;
num(frame,8) = ChainplusF;
else
while ~ismembertol(num(b,5),num(frame,9),0.001) && ~isempty(num(b,5))
b=b+1;
if ismembertol(num(b,5),num(frame,9),0.001)
W = num(b,4);
ChainplusF = Originalchainage + W;
num(frame,8) = ChainplusF;
end
end
end
else
num(frame,8) = '-';
end
%**********************************************************************************************
% Write the image array to the output file, if requested.
if writeToDisk
% Construct an output image file name.
outputBaseFileName = sprintf('Frame %d .JPEG',frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
% Stamp the name and frame number onto the image.
% At this point it's just going into the overlay, not actually getting written into the pixel values.
text(5, 15, outputBaseFileName, 'FontSize', 15);
% Extract the image with the text "burned into" it.
frameWithText = getframe(gca);
% frameWithText.cdata is the image with the text actually written into the pixel values.
% Write it out to disk.
imwrite(frameWithText.cdata, outputFullFileName, 'jpg');
end
while ~ismembertol(num(b,5),num(frame,9),0.001) && ~isempty(num(b,5))
What happens if that is never false inside your data?
The loop will keep adding b=b+1 to compare every single row till it reaches the last row of the excel without any values then it should stop looping and continue back to the 1st step of b=1 and start comparing again. I want the while loop to stop when it does not manage to find any values within tolerance and when that cell value has no vlue to compare.
But it does not stop looping then. It adds 1 to b and tries to access at row b when that is beyond the size of the array.
but there is the condition ~isempty(num(b,5)) so when it access the row and realises there are no more values in the array then shouldnt it exit while loop?
I believe the problem you have is the fact that even though there is no number in the cell, the cell is not empty. It is not possible (at least, not that I know of in Matlab) to have an colums in a matrix have unequal lengths. When xlsread reads an spreadsheet file with columns of different lengths, it seems that xlsread fills in the empty cells with 'NaN' (I just tried an example to verify this). Hence, the check ~isempty(num(b,5)) will find NaN in cells that don't have numbers and hence b+1 will keep happening.
Try using ~isnan(num(b,5)) instead of and see if that works ~isempty(num(b,5))
I have tried but it still gives me the same error. I realised that the error comes out when the code could not find a comparison when there is a sudden jump in value after looping all the rows.
I've not tried to understand the code fully, but it looks like it's trying to do something fairly simple in a complicated way.
Perhaps, the best course of action is to forget the original code and for you to explain clearly what you're trying to do, not in term of for / while loops but in a more generic way. Then we can give you an efficient way of doing it.
Also, which version of matlab are you using that you are still using xlsread?
Scalar numeric array entries are never empty.
In Excel itself, it is true that if you attempt to access anything beyond the last filled row, that the cell will be reported as empty. Cells in Excel are more similar to MATLAB cell arrays, in which each has an individual type (and formatting instructions) and so can individually be empty. But in MATLAB, empty always means an array with no elements in it, such as zeros(0,0) also known as [], where there is no "there" there, and a numeric entry of size 1 x 1 has one element not zero elements and so is never considered empty. A numeric entry can have value -inf or +inf or any of thousands of technically different Not A Number values, but if it has at least one value then it is not empty for MATLAB purposes.
My matlab version is 2017b. What I’m trying to do is develop a code which allows me to select an excel file and a mp4 video. The code will extract the frame and write each frame timing into the same excel file then use this timing to compare with the value in column 5 of the excel file. If it matches then write the value from column 4 into column 8. If upon comparing all the rows of the excel file and failed to find any similar values then we can just put a text ‘NA’ into column 8 of that row.

Sign in to comment.

 Accepted Answer

I've managed to resolve this issue by using a for loop to run through all the element in the array instead of using the while loop so this removes the need to check if i have reached the end of the array as i loop my comparison. Here's my code:
Thanks to those who tried to help as well.. really got me thinking.
for k=1:numel(num(:,5))
if ismembertol(num(k,5),num(frame,9),0.001)
W = num(k,4);
ChainplusF = Originalchainage +W;
num(frame,8) = ChainplusF;
end
end

1 Comment

As I wrote in a comment, you're overcomplicating! The above can be simplified to:
found = ismembertol(num(:, 5), num(frame, 9), 0.001);
num(found, 8) = Originalchainage + num(found, 4);
No need for loops or if.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 5 Nov 2019

Commented:

on 6 Nov 2019

Community Treasure Hunt

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

Start Hunting!