How can I solve this question

조회 수: 3 (최근 30일)
A M Saif Ben Kabir Amit
A M Saif Ben Kabir Amit 2020년 4월 24일
편집: Pravin Jagtap 2020년 4월 27일
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
KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 4월 24일
There are sufficient hints in the question, any specific issue?
A M Saif Ben Kabir Amit
A M Saif Ben Kabir Amit 2020년 4월 24일
I manages to code like this.But after Run it show somwthing like this
Function 'PBTask4p2b_f' has already been declared within this scope.
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Pravin Jagtap
Pravin Jagtap 2020년 4월 27일
편집: Pravin Jagtap 2020년 4월 27일
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.
Hope this will help you.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by