Why won't CheckStatus(1, 1) return 0?

function onTime = CheckStatus(noTraffic, gasEmpty)
% Assign onTime with true if noTraffic is true and gasEmpty is false
onTime = noTraffic(true), gasEmpty(false);
end

3 comentarios

per isakson
per isakson el 6 de Oct. de 2017
Editada: per isakson el 6 de Oct. de 2017
I assume this is a homework.
To find out why it doesn't work as expected, replace
onTime = noTraffic(true), gasEmpty(false);
by
onTime = noTraffic(true);
gasEmpty(false);
set a breakpoint, dbstop CheckStatus, call the function, CheckStatus(1, 1), step through the code and inspect the values of the variables.
so do this function onTime = CheckStatus(noTraffic, gasEmpty)
% Assign onTime with true if noTraffic is true and gasEmpty is false
onTime = noTraffic(true);
dbstop CheckStatus
gasEmpty(false);
end
Jan
Jan el 6 de Oct. de 2017
Editada: Jan el 6 de Oct. de 2017
You can either set a breakpoint in the editor, or type "dbstop CheckStatus" in the command window, not inside the code. Please read the documentation instead of guessing: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 6 de Oct. de 2017
Editada: Guillaume el 6 de Oct. de 2017
Before reading the documentation of dbstop, I'd say read the getting started tutorial of matlab or any introductory book.
I have no idea where the syntax for the line
onTime = noTraffic(true), gasEmpty(false);
came from. Certainly not from any matlab introductory course. The above does not check "if noTraffic is true", it does not check if "gasEmpty is false", and it certainly does not "and" any condition.
The above assigns the first element (since true is the same as 1) of noTraffic to onTime. The comma is equivalent to a line return, it just starts a new statement. That new statement accesses the 0th element of gasEmpty (since false is the same as 0). This will always error since matrix indexing starts at 1 in matlab. Even if did not error, nothing more would be done with that element, it would not be assigned to anything.
Testing if something is true of false is done using comparison operators. The result of such comparisons can then be linked using logical operators.

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 6 de Oct. de 2017

Editada:

el 6 de Oct. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by