Why does an 'catch me' error pop up while im running my while loop
Show older comments
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
on 5 Nov 2019
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.
XH K
on 5 Nov 2019
Image Analyst
on 5 Nov 2019
Can you attach the workbook with the paper clip icon?
XH K
on 5 Nov 2019
Walter Roberson
on 5 Nov 2019
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.
Walter Roberson
on 5 Nov 2019
while ~ismembertol(num(b,5),num(frame,9),0.001) && ~isempty(num(b,5))
What happens if that is never false inside your data?
XH K
on 5 Nov 2019
Walter Roberson
on 5 Nov 2019
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.
XH K
on 5 Nov 2019
Olawale Akinwale
on 5 Nov 2019
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.
Olawale Akinwale
on 5 Nov 2019
Try using ~isnan(num(b,5)) instead of and see if that works ~isempty(num(b,5))
XH K
on 5 Nov 2019
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?
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
on 6 Nov 2019
Accepted Answer
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!

