Error that does not make sense.

3 次查看(过去 30 天)
Luke Witherow
Luke Witherow 2022-3-11
评论: Voss 2022-3-11
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 个评论
Torsten
Torsten 2022-3-11
You still don't tell us what the error message from MATLAB is.
Is it a secret ?
Luke Witherow
Luke Witherow 2022-3-11
I am telling you that the error is from a line in a function which tells matlab to give an error when n>10.
if n>10
error("Alphabet size (n) canot exceed 10");
Hence my confusion when my values of n are less than 10 but this error still occurs.

请先登录,再进行评论。

回答(3 个)

Voss
Voss 2022-3-11
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
0 0 0
Alphabet size (n) canot exceed 10 Number of digits (d) cannot exceed alphabet size (n)
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 个评论
Luke Witherow
Luke Witherow 2022-3-11
Thank You this seems to have worked!
Voss
Voss 2022-3-11
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
Jan 2022-3-11
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.
  1 个评论
Luke Witherow
Luke Witherow 2022-3-11
This did not help, my values of n are 6, 8, and 9, none of which are greater than 10, but the code still gives an error at the line meant to give an error when n>10

请先登录,再进行评论。


Steven Lord
Steven Lord 2022-3-11
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
NOT all elements of n were greater than 5
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
At least one element of n was greater than 5
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
number = 1
textRepresentation = '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
isNumberTooLarge = logical
0
isTextTooLarge = textRepresentation > 10 % true
isTextTooLarge = logical
1
double(textRepresentation)
ans = 49
  1 个评论
Luke Witherow
Luke Witherow 2022-3-11
so are you thinking that this is an issue within the gen_array function?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by