

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Using the clear command inside a script could erase variables and values that you worked hard to get into the workspace. Good practice is to only issue the ...
Typology: Study notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!


(1) Scripts put their variables in the workspace.
Whenever a script is run, all variables that have values assigned are placed into the workspace. To clearly see this, start with a clear workspace. Enter » clear all to completely clear the workspace. Check that this has been done by entering » who » No variables should be listed in response. Now run wkshp6_ac1 by entering » wkshp6_ac Workshop 6, Activities 1 and 2 Please provide diary name ==> The script header and a request for a diary name appear. This particular script allows you to provide a diary name that will hold the contents of running the script. You can look at the script file later to see how this is done. Of interest is that you can use a script to turn the diary on and off. Enter a diary name of your choosing and press enter. The MATLAB command prompt appears. This means that the script has run!! Yet nothing appeared in the Command Window. So what did happen? Enter » who Your variables are: diary_name x y z Running the MATLAB script has put variables into your workspace. If you want, you can check to see what the variable types are (whos) and what values they contain. You might also want to check on whether the diary file has been placed in the current directory.
(2) Scripts will overwrite existing values for variables of the same name in the workspace.
Scripts use the active workspace in the same manner that you do when you operate from the command window. Any command issued in a script is the same as if you used the command directly. Sometimes unexpected results occur. One is that scripts will replace values for variables with the same name with their own values. To see this, enter the following » clear all » x = 7
x = 7 » who Your variables are: x to completely clear the workspace and then introduce the variable x with the value 7.
Now run wkshp6_ac1 again by entering » wkshp6_ac Workshop 6, Activities 1 and 2 Please provide diary name ==> Respond with a diary name of your choice and enter » who Your variables are: diary_name x y z The script again added its own variables. Check on the value for x. Enter » x x = 1 Running the MATLAB script changed the value of x from 7 to 1. This can be dangerous because there is no warning that the script was using a variable name the same as one you already had in the workspace.
(3) Scripts will use existing variables of the same name in the workspace.
This issue is important in designing and debugging scripts. A common method for developing scripts is to work a sample problem with the diary on and then edit the diary to create a script. This is the method illustrated in Workshop 5. However, there is a problem with this approach in that the workspace contains the variables that were used to develop the script (same names). Improper editing of the diary file to create the script can result in a situation such as the following. Enter » clear all » x = 1; » y = 2; » z = 3; » wkshp6_ac Workshop 6, Activity 3 Please provide diary name ==> Respond with a diary name of your choice and press enter. The command prompt appears, indicating that the script has executed without any problems.
Now try (provide a diary name of your choice when requested) » clear all » wkshp6_ac Workshop 6, Activity 3 Please provide diary name ==> ??? Undefined function or variable 'x'. Error in ==> c:\temp\wkshp6_ac3.m On line 23 ==> d = x*y;