# MATLAB Programming/Basic Reading and Writing data from a file

[MATLAB Programming\|/MATLAB Programming]m]
Chapter 1: MATLAB ._.
    Fundamentals of MATLAB
    MATLAB Workspace
    MATLAB Variables
    *.mat files
Chapter 2: MATLAB Concepts
    MATLAB operator
    Data File I/O
Chapter 3: Variable Manipulation
    Numbers and Booleans
    Strings
    Portable Functions
    Complex Numbers
Chapter 4: Vector and matrices
    Vector and Matrices
    Special Matrices
    Operation on Vectors
    Operation on Matrices
    Sparse Matrices
Chapter 5: Array
    Arrays
    Introduction to array operations
    Vectors and Basic Vector Operations
    Mathematics with Vectors and Matrices
    Struct Arrays
    Cell Arrays
Chapter 6: Graphical Plotting
    Basic Graphics Commands
    Plot
    Polar Plot
    Semilogx or Semilogy
    Loglog
    Bode Plot
    Nichols Plot
    Nyquist Plot
Chapter 7: M File Programming
    Scripts
    Comments
    The Input Function
    Control Flow
    Loops and Branches
    Error Messages
    Debugging M Files
Chapter 8: Advanced Topics
    Numerical Manipulation
    Advanced File I/O
    Object Oriented Programming
    Applications and Examples
    Toolboxes and Extensions
Chapter 9: Bonus chapters
    MATLAB Benefits and Caveats
    Alternatives to MATLAB
    [MATLAB_Programming/GNU_Octave|What is Octave= (8) hsrmonic functions]
    Octave/MATLAB differences

## MATLAB file types

There are two file types used by MATLAB namely .m and .mat files
* .m / .mat files: Standard MATLAB files (Most functions in .mat can be supported by the older version off MATLAB)
* .mlx files: Live Code File Format. This format were just introduced on MATLAB version R2016a. This type of files storing the live scripts/live functions that uses the Live Code file format.

## Import functions

The function that usually are used to import data into MATLAB : importdata(filename)
importdata(filename) can loads data into array.

### Loading Data

load(filename) can loads data from filename. The files can be text, images , and even audio files.
The load command is used to load data from a file into the current workspace.
   * Load all variables from the file mySave.mat into the current workspace.
   * Load just the variables myData1 and myData2.
   * Load all myData variables.
   * Get a cell array of variables in saved file.

### Saving Data

The save command is used to save workspace data to a file.
   * Save all workspace data to the file mySave.mat in the current directory.
   * Save just the variables myData1 and myData2 to mySave.mat.
   * Save all myData variables to mySave.mat.
   * Save all myData variables to a mySave.mat file compatible with version 6 of MATLAB.
   * Save all myData variables to an ASCII file.
   * Append new variables to the data file.

## Excel Spreadsheets I/O

Since analyzing data is one of the more common motivations for using input output I will start with reading and writing from a spreadsheet. I cover the command line first since it is often necessary to import the data while an m-function is being evaluated.

### Reading Excel Spreadsheets

MATLAB makes it easy to read from an Excel spreadsheet. It has the built-n command readcell
Let say, you have an Excel file (Preferably latest version with file format .xlsx ) , In this example, you have a table with data shown as below saved as Class Marks.xlsx in any place of your liking
| Name | Marks |
| Ali | 93 |
| Chin | 47 |
| Sammy | 74 |
| Dorothy | 96 |
| Huat | 94 |
| Anna | 38 |
To read the contents , you need to type as follows:
Pro tips: If you are unsure of the file location, right click of the file that you saved and select Properties and look for Locations in General tabs. You can change mydir into your selected locations
It will shows the output as follows
This line of code reads Class Marks.xlsx (from the saved directory - mydir) and places it in an identical array inside MATLAB called A. You can then manipulate the array A in any way you want.

#### Reading from certain range in the text files

From table above just now, we expand to more rows and columns as shown below in Sheet named Class1A. Note that A-> H and 1->11 are for references
In this example, we are interested , we interested in Huat marks and make an array B to extract his marks
| | A | B | C | D | E | F | G | H |
| 1 | Name | English | Maths | Science | Art | Music | Malay | Moral |
| 2 | Ali | 71 | 54 | 65 | 27 | 65 | 54 | 96 |
| 3 | Chan | 14 | 96 | 50 | 40 | 17 | 99 | 95 |
| 4 | Sammy | 17 | 15 | 72 | 24 | 55 | 15 | 26 |
| 5 | Dorothy | 89 | 35 | 45 | 14 | 87 | 52 | 65 |
| 6 | Huat | 82 | 90 | 15 | 65 | 42 | 82 | 90 |
| 7 | Anna | 12 | 77 | 25 | 63 | 46 | 14 | 31 |
| 8 | Lee | 77 | 22 | 49 | 91 | 43 | 89 | 66 |
| 9 | Muthu | 61 | 25 | 38 | 65 | 43 | 35 | 89 |
| 10 | Ismail | 70 | 70 | 79 | 75 | 81 | 44 | 98 |
| 11 | Th'ng | 42 | 92 | 99 | 14 | 46 | 12 | 24 |
The comments explained how to select certain range of wanted data.
The B array will show following results

### Writing Excel Spreadsheets

The writecell command allows data to be added into the Excel files.
First, we create data for our student Juan.as followed:
Later on , use the writecell command (Make sure the Excel file is closed to prevent error message following)
When you open back the Excel files: you should see Juan with his marks
| | A | B | C | D | E | F | G | H |
| 1 | Name | English | Maths | Science | Art | Music | Malay | Moral |
| 2 | Ali | 71 | 54 | 65 | 27 | 65 | 54 | 96 |
| 3 | Chan | 14 | 96 | 50 | 40 | 17 | 99 | 95 |
| 4 | Sammy | 17 | 15 | 72 | 24 | 55 | 15 | 26 |
| 5 | Dorothy | 89 | 35 | 45 | 14 | 87 | 52 | 65 |
| 6 | Huat | 82 | 90 | 15 | 65 | 42 | 82 | 90 |
| 7 | Anna | 12 | 77 | 25 | 63 | 46 | 14 | 31 |
| 8 | Lee | 77 | 22 | 49 | 91 | 43 | 89 | 66 |
| 9 | Muthu | 61 | 25 | 38 | 65 | 43 | 35 | 89 |
| 10 | Ismail | 70 | 70 | 79 | 75 | 81 | 44 | 98 |
| 11 | Th'ng | 42 | 92 | 99 | 14 | 46 | 12 | 24 |
| 12 | Juan | 43 | 67 | 88 | 45 | 35 | 92 | 65 |

## Text files I/O
### Reading Text Files

If a file is not an excel spreadsheet, it can still be read using "load" function:
This works only if the text is entirely numerical, without special formatting. Otherwise you get an 'unrecognized character' error.
The easiest way to write to a non-excel file, or using MATLAB 6.5 or less, is to use the same code as that for writing excel files but change the extension. Usually there are no formatting difficulties with plain text files.
For reading more general text files, MATLAB does not have a function to do it easily (unless you have excel), but you can read very general text files (with different delimiters for both cells and text within cells) using the "textread.m" function in the MATLAB file exchange (do a google search to find it). You can also try to use fscanf if the formatting is consistent enough (i.e. consistent numbers of spaces, no mixing of strings and numbers in columns, and so on).

# MATLAB File I/O: from the Graphical User Interface

MATLAB contains a nice GUI application that will guide you through importing data from any recognized data file (usually .mat, .txt, or .xls on a Windows system). To use it, go to file > import data, and select the file you want. Then, choose what column separators are present (by selecting the appropriate radio button). Finally, click "next".
MATLAB saves the variable under a name similar to that of the file, but with modifications to make it conform with MATLAB syntax. Spaces are omitted, plusses and minuses are turned into other characters. To see the name MATLAB generated (and probably change it) type "who" in the command prompt.

## External Resources

ControlTheoryPro.com
MatlabCodes.Webs.com