Error that does not make sense.
12 views (last 30 days)
Show older comments
I am calling a function which is meant to give an error if n is greater than 10; however, the values I am assigning to "n" are less than 10, but the error is still coming up and I do not understand why.
ndarray = [6, 8, 9;
4, 8, 6] ;
%top row is possible n values, bottom row is possible d values.
[numberRounds1] = crack_code(ndarray(1,1),ndarray(2,1)); %first set of n and d values
[numberRounds2] = crack_code(ndarray(1,2),ndarray(2,2)); %second set of n and d values
[numberRounds3] = crack_code(ndarray(1,3),ndarray(2,3)); %third set of n and d values
Here is the function these values are needed for:
[numberRounds] = crack_code(n,d)
Within this function I am calling the function that is giving the error.
Here is the part of the function that is giving the eroor.
% Sanity checks
if n>10
error("Alphabet size (n) canot exceed 10");
end
if d>n
error("Number of digits (d) cannot exceed alphabet size (n)");
end
7 Comments
Torsten
on 11 Mar 2022
You still don't tell us what the error message from MATLAB is.
Is it a secret ?
Answers (3)
Voss
on 11 Mar 2022
Is it possible there is another crack_code.m that supercedes this one on the path so that that other one is being called instead of this one? You can do the following to find out:
which crack_code -all
Or is it possible that ndarray changes before it is used in crack_code?
As it is, this function seems to work ok:
ndarray = [6, 8, 9, 11 7;
4, 8, 6, 10 9];
%top row is possible n values, bottom row is possible d values.
for jj = 1:size(ndarray,2)
try
[numberRounds] = crack_code(ndarray(1,jj),ndarray(2,jj));
disp(numberRounds);
catch ME
disp(ME.message);
end
end
function [numberRounds] = crack_code(n,d)
% Sanity checks
if n>10
error("Alphabet size (n) canot exceed 10");
end
if d>n
error("Number of digits (d) cannot exceed alphabet size (n)");
end
numberRounds = 0;
end
8 Comments
Voss
on 11 Mar 2022
You're welcome!
If you don't mind, please mark the answer as 'Accepted' so that others know the problem seems to have been resolved.
Jan
on 11 Mar 2022
You can be completely sure, that the error occurs only, if the conditions are met. Matlab is not tired or evil, but a deterministic system. Use the debugger to find out, what's going on: Set a breakpoint in the two lines containing the error() commands. Or let Matlab stop at all errors automatically:
dbstop if error
Now let Matlab run and when it stops, check the condition manually.
You did not mention, if the n>10 or d>n condtionen caused the error. But if you check the vcalues of d and n, this will be clear immediately.
A possible source of the error is, that you have two versions of the function and run not the one you are expecting. Check this by:
which crack_code -all
and the same for the main script or function.
Steven Lord
on 11 Mar 2022
At first I thought you might be running into the common non-scalar if condition problem. From the documentation of the if keyword: "if expression, statements, end evaluates an expression, and executes a group of statements when the expression is true. An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false." [Bold emphasis added.]
So in the statement:
% Block commenting so I can run code later in this Answer
%{
if n > 10
%}
that condition is satisfied only if ALL the elements of n are greater than 10. If you have a million elements in n that are greater than 10 and one that is not greater than 10, the if condition is not satisfied.
If you want that condition to be satisfied if ANY of the values in the expression are true, use the any function.
n = 1:10;
if n > 5 % not satisfied
disp("All elements of n were greater than 5")
else
disp("NOT all elements of n were greater than 5")
end
if any(n > 5) % Satisfied
disp("At least one element of n was greater than 5")
else
disp("NONE of the elements of n were greater than 5")
end
But reading more carefully, I wonder if the data you're showing us is the data on which you're operating. Are you passing a number into your function or the text representation of that number?
number = 1
textRepresentation = '1'
The variable named number contains a value less than 10. The variable named textRepresentation contains a character whose character code is greater than 10.
isNumberTooLarge = number > 10 % false
isTextTooLarge = textRepresentation > 10 % true
double(textRepresentation)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!