How can I solve this question
2 views (last 30 days)
Show older comments
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.
For example: >> PBTask4p2a_f(200)
COUNTER = 200
>> PBTask4p2b_f(600)
COUNTER = 800
>> PBTask4p2a_f(300)
COUNTER = 0
2 Comments
Answers (1)
Pravin Jagtap
on 27 Apr 2020
Edited: Pravin Jagtap
on 27 Apr 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.
https://www.mathworks.com/matlabcentral/answers/420960-how-to-use-global-variables-inside-a-function
Hope this will help you.
0 Comments
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!