Error that does not make sense.

12 visualizaciones (últimos 30 días)
Luke Witherow
Luke Witherow el 11 de Mzo. de 2022
Comentada: Voss el 11 de Mzo. de 2022
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 comentarios
Torsten
Torsten el 11 de Mzo. de 2022
You still don't tell us what the error message from MATLAB is.
Is it a secret ?
Luke Witherow
Luke Witherow el 11 de Mzo. de 2022
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.

Iniciar sesión para comentar.

Respuestas (3)

Voss
Voss el 11 de Mzo. de 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
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 comentarios
Luke Witherow
Luke Witherow el 11 de Mzo. de 2022
Thank You this seems to have worked!
Voss
Voss el 11 de Mzo. de 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.

Iniciar sesión para comentar.


Jan
Jan el 11 de Mzo. de 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.
  1 comentario
Luke Witherow
Luke Witherow el 11 de Mzo. de 2022
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

Iniciar sesión para comentar.


Steven Lord
Steven Lord el 11 de Mzo. de 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
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 comentario
Luke Witherow
Luke Witherow el 11 de Mzo. de 2022
so are you thinking that this is an issue within the gen_array function?

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by