Lab 9: File structure and Read/Write from/to files

Files and Folders in MATLAB

Here are some commands that help us navigate around files and folders (directories), i.e, the local file structure. Try each one of them in your MATLAB's command window. Examine the created variables in your Workspace as needed.
  • filesep: directory separator (Macs/PCs)
  • pwd: Print Working Directory
  • mkdir(newFolderName): creates a new folder with given name
  • dir: returns a structure with the following fields for each file and folder in the working folder (directory):
    1. name
    2. folder
    3. date
    4. bytes
    5. isdir
    6. datenum
  • An other form of the dir command is available:
    listing = dir(name)
    The input to this command is the relative or absolute name of a folder. The file name can include the wild character '*'. So, if we want to list all the files with names ending in ".jpg" in the working directory, we can issue the command:
    >allJPGs = dir('*.jpg');

General Tips

  1. Use [ ] to build the string names for files/folders you need
  2. Use mkdir to create new folders once you have their string names
  3. Always use a file separator between folder names and file names

Some examples

  • How to get the full path string of the current folder:
    [pwd filesep]
    evaluates to (in my case)
    ['/Users/skakavou/Desktop/assign7_exercises/' '/']
    which is the string
    '/Users/skakavou/Desktop/assign7_exercises/'
  • Making a new folder? Let's say it is called 'testFolder'.
    First, generate the string for the new folder's name:
    [pwd filesep 'testFolder'] ==>
    '/Users/skakavou/Desktop/assign7_exercises/testFolder'
    Then, use mkdir to create the new folder.
  • Creating a new file in the newly created folder? Let's say your new file is named 'smile.m'.
    First, generate the string that contains the full path string to your new file:
    [pwd filesep 'testFolder' filesep 'smile.m'] ==>
    '/Users/skakavou/Desktop/assign7_exercises/testFolder/smile.m'
    Then, you can use that string to create your new file.

Task 1: Listing all the MATLAB files

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/skakavou/Desktop/assign7_exercises/blueFile.m
/Users/skakavou/Desktop/assign7_exercises/brownFile.m
/Users/skakavou/Desktop/assign7_exercises/greenFile.m
/Users/skakavou/Desktop/assign7_exercises/lab9warmup.m
/Users/skakavou/Desktop/assign7_exercises/magentaFile.m
/Users/skakavou/Desktop/assign7_exercises/orangeFile.m
/Users/skakavou/Desktop/assign7_exercises/pinkFile.m
/Users/skakavou/Desktop/assign7_exercises/redFile.m

Task 2: Writing a new image to a new file in a new folder

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, write the negative marathon image into the negatives folder and call it 'negativeMarathonImage.jpg' (Hint: use imwrite).
Now that you had some practice navigating your way through MATLAB files and folders, you can work on Exercise 1 in Assign 7.