- Matlab Tutorial
- MATLAB Advanced
- MATLAB Useful Resources
- Selected Reading
How to read/load a data file into matlab code. Learn more about read data file.
So far, we have used MATLAB environment as a calculator. However, MATLAB is also a powerful programming language, as well as an interactive computational environment.
In previous chapters, you have learned how to enter commands from the MATLAB command prompt. MATLAB also allows you to write series of commands into a file and execute the file as complete unit, like writing a function and calling it.
The M Files
MATLAB allows writing two kinds of program files −
- Scripts − script files are program files with .m extension. In these files, you write series of commands, which you want to execute together. Scripts do not accept inputs and do not return any outputs. They operate on data in the workspace.
- Functions − functions files are also program files with .m extension. Functions can accept inputs and return outputs. Internal variables are local to the function.
You can use the MATLAB editor or any other text editor to create your .mfiles. In this section, we will discuss the script files. A script file contains multiple sequential lines of MATLAB commands and function calls. You can run a script by typing its name at the command line.
Creating and Running Script File
To create scripts files, you need to use a text editor. You can open the MATLAB editor in two ways −
- Using the command prompt
- Using the IDE
If you are using the command prompt, type edit in the command prompt. This will open the editor. You can directly type edit and then the filename (with .m extension)
The above command will create the file in default MATLAB directory. If you want to store all program files in a specific folder, then you will have to provide the entire path.
Let us create a folder named progs. Type the following commands at the command prompt (>>) −
If you are creating the file for first time, MATLAB prompts you to confirm it. Click Yes.
Alternatively, if you are using the IDE, choose NEW -> Script. This also opens the editor and creates a file named Untitled. You can name and save the file after typing the code.
Type the following code in the editor −
After creating and saving the file, you can run it in two ways −
- Clicking the Run button on the editor window or
- Just typing the filename (without extension) in the command prompt: >> prog1
The command window prompt displays the result −
Example
Create a script file, and type the following code −
When the above code is compiled and executed, it produces the following result −
Description
[A,B,C,...] = textread(filename,format)
reads data from the file filename
into the variables A
, B
, C
, and so on, using the specified format
, until the entire file is read. Specify filename
and format
inputs as character vectors or string scalars. textread
is useful for reading text files with a known format. textread
handles both fixed and free format files.Note
When reading large text files, reading from a specific pointin a file, or reading file data into a cell array rather than multipleoutputs, you might prefer to use the
textscan
function.textread
matches and converts groups of charactersfrom the input. Each input field is defined as a group of non-white-spacecharacters that extends to the next white-space or delimiter character,or to the maximum field width. Repeated delimiter characters are significant,while repeated white-space characters are treated as one.The
format
input, specified as a character vector or a string scalar, determines the number and types of return arguments. The number of return arguments is the number of items indicated by the contents of format
. format
supports a subset of the conversion specifiers and conventions of the C language fscanf
routine. Values for format
are listed in the table below. White-space characters in format
are ignored.format | Action | Output |
---|---|---|
Literals (ordinary characters) | Ignore the matching characters. For example, in a file that has Dept followed by a number (for department number), to skip the Dept and read only the number, use 'Dept' in the format specifier format . | None |
%d | Read a signed integer value. | Double array |
%u | Read an integer value. | Double array |
%f | Read a floating-point value. | Double array |
%s | Read a white-space or delimiter-separated text. | Cell array of character vectors |
%q | Read double quoted text, ignoring the quotes. | Cell array of character vectors |
%c | Read characters, including white space. | Character array |
%[...] | Read the longest group of characters containing charactersspecified in the brackets. | Cell array of character vectors |
%[^...] | Read the longest nonempty group of characters containingcharacters that are not specified in the brackets. | Cell array of character vectors |
%*... | Ignore the matching characters specified by * . | No output |
%w... | Read field width specified by w . The %f formatsupports %w.pf , where w is thefield width and p is the precision. |
[A,B,C,...] = textread(filename,format,N)
reads the data, reusing the format specified in format
, N
times, where N
is an integer greater than zero. If N
is smaller than zero, textread
reads the entire file.[...] = textread(...,param,value,...)
customizes textread
using param/value
pairs,as listed in the table below. param | value | Action | |
---|---|---|---|
bufsize | Positiveinteger | Specifies the maximum length of the character vector,in bytes. Default is 4095 . | |
commentstyle | matlab | Ignores characters after % . | |
commentstyle | shell | Ignores characters after # . | |
commentstyle | c | Ignores characters between /* and */ . | |
commentstyle | c++ | Ignores characters after // . | |
delimiter | One or morecharacters | Act as delimiters between elements. Default is none. | |
emptyvalue | Scalar double | Value given to empty cells when reading delimited files.Default is 0. | |
endofline | Single characteror 'rn' | Character that denotes the end of a line. Defaultis determined from file | |
expchars | Exponentcharacters | Default is eEdD . | |
headerlines | Positiveinteger | Ignores the specified number of lines at the beginningof the file. | |
whitespace | Anyfrom the list below: | Treats vector of characters aswhite space. Default is ' bt' . | |
' ' | Space Backspace Newline Carriage return Horizontaltab |
Note
When
textread
reads a consecutive seriesof whitespace
values, it treats them as one whitespace. When it reads a consecutive series of delimiter
values,it treats each as a separate delimiter.