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

조회 수: 9 (최근 30일)
XH K
XH K 2019년 11월 5일
댓글: Guillaume 2019년 11월 6일
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
Walter Roberson
Walter Roberson 2019년 11월 5일
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 2019년 11월 6일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

XH K
XH K 2019년 11월 6일
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
Guillaume
Guillaume 2019년 11월 6일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by