Matlab Programming Question 1 (c) Every MATLAB environment contains a workspace.
ID: 3914309 • Letter: M
Question
Matlab Programming Question 1 (c) Every MATLAB environment contains a workspace. For the most part, the workspace contains and manages your variables. Does the workspace differ when executing the following: 1. commands in the command window 2. a script 3. a function If so, how? [3 marks] (d) Explain how global variables in MATLAB be used to share information be- tween workspaces? What is the life-time of a global variable? [2 marks] Question 2 (d) Write equivalent code to the one shown below by replacing the while loop with a for loop. while x zip_script 1 47 4 6 2 9 5 Hint: Avoid assigning the result in to a vector before printing them [5 marks]Explanation / Answer
Please ask 1 question at a time
Answer
c) 1. Commands in the command window use the same workspace as provided, which is also called base workspace.
2.scripts use the same workspace,i.e. Base workspace.Variables used in scripts, after its execution are available at command window.Scripts can also make use of variables defined at the command editor.
3.Functions dont use the base workspace, they use a workspace which is local to each function, which is called the local workspace. Ordinary variables used in function are not available for use to scripts and commands in editor.
d)If we declare a variable as global in different workspaces, then those workspaces each have a copy of that global variable. Changes made to that variable in any of the workspaces will be reflected in all the other workspaces as well. Global variables have their own workspace called the global memory. Global variables are helpful if the variables contain large amount of data and are used in different scripts and function. By defining them as global, we essentially have declared them only once, whiwh saves time and memory.
The lifetime of global variables is indefinite. They are cleared from global memory explicitly by using the following command:
clear global variableName