# How can I solve this question

7 visualizaciones (últimos 30 días)
A M Saif Ben Kabir Amit el 24 de Abr. de 2020
Editada: Pravin Jagtap el 27 de Abr. de 2020
1) In MATLAB command window, declare the variable COUNTER to be global with the global statement:
global COUNTER; % Declare COUNTER to be global.
COUNTER = 0; % Specify initial value.
This global variable is used to store the sum of the work pieces that two trucks have collected.
2) Declaring a variable to be global means that it is stored in the global workspace and not in a local workspace. Any function wanting to use this global variable must also include the statement. This statement must be placed at the beginning of the script or the function code before any attempt to use the variable is made. global COUNTER; % This tells MATLAB to use the global % variable and not create a local variable Create two user-defined MATLAB functions called PBTask4p2a_f.m (this function is for Truck A to input its count) and PBTask4p2b_f.m (for Truck B to input its count), respectively, to add the count from each truck to the global variable COUNTER. If the variable COUNTER exceeds 1000 after being added, the two programs should reset the COUNTER to 0. Call the two functions with count for each truck and display the updated value of COUNTER in command window.
COUNTER = 200
COUNTER = 800
COUNTER = 0
##### 2 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
A M Saif Ben Kabir Amit el 24 de Abr. de 2020
I manages to code like this.But after Run it show somwthing like this
global counter; % Declare COUNTER to be global.
counter = 0; % Specify initial value.
global counter; % This tells MATLAB to use the global
% variable and not create a local variable
a=input('truck a counter: ');
b=input('truck b counter: ');
function counter=counter+a+b
if counter>1000
counter =0
end
end
end

Iniciar sesión para comentar.

### Respuestas (1)

Pravin Jagtap el 27 de Abr. de 2020
Editada: Pravin Jagtap el 27 de Abr. de 2020
Hello,
As mentioned in the above comments, there are enough hints in question. This exercise will benefit you in understanding 'global' variables, their scope, and how to use them to achieve the task at hand.
Please refer to the following links to understand the usage of the 'global' variable.
##### 0 comentariosMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

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

### Community Treasure Hunt

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

Start Hunting!

Translated by