Physics 281 - Computational Physics

Wednesday/Friday Section

Fall 2009

How-to Guide for Copying m-files and figures

Our exercises require you to copy your m-file scripts and (sometimes) resulting figures to a directory within my home directory so that I can evaluate them. The purpose of this short guide is to provide some easy instruction on how to do this. In this example, I assume that you need to copy a file into the subdirectory where all your assignments will be stored. You will be given a subdirectory with your user name to write your scripts. In this example, the username is astudent. Sometimes, it is convenient to make an additional subdirectory in your area as well.

Option 1 - Use file copying commands from MATLAB command window

I think this may be the easiest option. As you do your work, your files (no matter what they are called) are contained in your home directory on the system or in some subdirectory of this that you have chosen to use. MATLAB provides some special commands (mostly similar to UNIX commands) that can be entered on the command line to copy files.

1. Note that all commands described here are entered into the MATLAB command window at the >> prompt.

2. Type:

ls

to see a list of the files in your current working directory for MATLAB. You should see the file (or files) you wish to copy.

3. Sometimes I suggest that you should make a subdirectory with your user name to contain the submitted files. Suppose that ~schloerb/ph281/astudent is the directory for submission. You can create a subdirectory called ExerciseX with the mkdir command by typing:

mkdir ~schloerb/ph281/astudent/ExerciseX

4. Now copy the files you wish to submit. To copy your m-file into the new directory type:

copyfile new_file_name.m ~schloerb/ph281/astudent/ExerciseX

where the new_file_name.m is the name of the submitted file.

5. Finally you can check to make sure that the file is where you think it is using ls. Type:

ls ~schloerb/ph281/astudent/ExerciseX

Option 2 - MATLAB "Save As..."

A second option is to use the "Save As" option under the File menu in MATLAB. For m-files this is straightforward. You need to move up a directory from your home directory and then select my home directory (schloerb) and follow the directory tree to the assigned place. This is how many students did this the first day.

There are some gotcha's with this approach. First, using Save As with scripts causes MATLAB to change its path to the directory you copied to. This means that if you edit the file more, you will change it! Another problem is that saving figures this way is a bit harder since the save directory is much less obvious to find. You must move up the directory tree using the ".." directory before you can select my home directory. But, with these ideas in mind, this is a reasonable way to proceed.

Option 3 - Unix Commands

It would be great if people would all learn UNIX, and it would be a good career move for you to learn at least some basic commands for file copying and the like. Here's a procedure to use UNIX to submit a copy of your scripts.

1. Open a terminal window from the applications menu. You will enter the commands that follow in this UNIX terminal window.

2. Check to see list of files to be sure that you are in the directory with the correct file. Type:

ls

3. If necessary, make a directory to submit your files to. Type:

mkdir ~schloerb/ph281/astudent/ExerciseX

4. Now copy your file to the directory for submission, using a new name if required. Type:

cp new_file_name.m ~schloerb/ph281/astudent/ExerciseX/

5. You can check to see if your file is there using the ls command. Type:

ls ~schloerb/ph281/astudent/ExerciseX

to verify that your file is present.

Ph281 Home

Ph281 Assignments Page