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

9 views (last 30 days)
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
Walter Roberson
Walter Roberson on 5 Nov 2019
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.
XH K
XH K on 6 Nov 2019
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

XH K
XH K on 6 Nov 2019
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
Guillaume
Guillaume on 6 Nov 2019
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

Community Treasure Hunt

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

Start Hunting!