Lab 9: File Input/Output

File Input/Output

Today we'll practice reading from and writing to files. We will start with some commands that help us navigate around files and folders.
  • filesep: directory separator (Macs/PCs)
  • pwd: Print Working Directory
  • dir: returns a structure with these fields for each file/folder:
    1. name
    2. folder
    3. date
    4. bytes
    5. isdir
    6. datenum
  • mkdir(newFolderName): creates a new folder with given name

Tips

  • Current folder is usually:
    [pwd filesep]
    e.g., ['/Users/labuser/Desktop/assign7_exercises' '/'] ==> '/Users/labuser/Desktop/assign7_exercises/'
  • Making a new folder? Let's say it is called 'silly':
    [pwd filesep 'silly']==>
    '/Users/labuser/Desktop/assign7_exercises/silly'
  • Creating a new file in a new folder? Let's say your new file is 'smile.m':
    [pwd filesep 'silly' filesep 'smile.m']==>
    '/Users/labuser/Desktop/assign7_exercises/silly/smile.m'

Task 1

Create a new file called lab9warmup.m in your assign7_exercises folder. Write a few lines of MATLAB code to display all the *.m files in the assign7_exercises folder. Here is what your output should look like (note that the complete path of each file is shown):
>> lab9warmup
/Users/labuser/Desktop/assign7_exercises/blueFile.m
/Users/labuser/Desktop/assign7_exercises/brownFile.m
/Users/labuser/Desktop/assign7_exercises/greenFile.m
/Users/labuser/Desktop/assign7_exercises/lab9warmup.m
/Users/labuser/Desktop/assign7_exercises/magentaFile.m
/Users/labuser/Desktop/assign7_exercises/orangeFile.m
/Users/labuser/Desktop/assign7_exercises/pinkFile.m
/Users/labuser/Desktop/assign7_exercises/redFile.m

Task 2

Take a look at the Marathon Monday image provided in your assign7_exercises folder.
marathonImage = imread('marathon.jpg'); % a good place for a semicolon
imshow(marathonImage)
Now, create the negative image of the marathon image:
negative_marathon = 255 - marathonImage; % another excellent place for a semicolon
imshow(negative_marathon)
Here comes the MATLAB file stuff:
  • Using MATLAB, create a new folder called negatives inside assign7_exercises.
  • Using MATLAB (hint: imwrite), write the negative marathon image into the negatives folder and call it 'negativeMarathonImage.jpg'.
Now that you had some practice navigating your way through MATLAB files and folders, you can work on Exercise 1 in Assign 7.