If you're new to MATLAB, you can refer to Mytour's articles on MATLAB syntax for more information. Continue reading the article below to learn about data input techniques in MATLAB.
Data Input Techniques in MATLAB
The importdata function enables you to load multiple data files with various formats in MATLAB. Here are 5 importdata functions in MATLAB along with descriptions:
By default, Octave does not support the importdata() function, so you will need to search and install this package for the examples below to work on Octave's installed function.
Example 1
Below is an example of loading and displaying an image file. Create a script file and input the following code snippet:
File name = 'smile.jpg';
Data = importdata(filename);
Display image(Data);
When running the file, MATLAB will display the image file. However, you must store this image file in the current directory.
Example 2
In this example, we're importing a text file and specifying the delimiter and column headers. Create an ASCII file separated by spaces with column headers as weeklydata.txt.
The weeklydata.txt file looks like this:
Create a script file and input the following code snippet:
Upon running the file, it will display the following result:
Example 3
In this example, we're inputting data into the clipboard.
Copy the line below into the clipboard:
Simplicity in Mathematics
Create a script file and input the following code snippet:
A = importdata('-pastespecial')
Upon running the file, it will return the following result:
A =
'Mathematics is easy'
Low-Level File I/O
The importdata function is a high-level data input function in MATLAB. Low-level file I/O functions in MATLAB allow control over reading and writing data to the file at the highest level. However, these functions require detailed information about your file to work efficiently. MATLAB provides the following functions to perform reading and writing data at the byte or character level:
Inputting Text Data Files with Low-Level I/O
MATLAB provides the following functions to input low-level text data files:
- The fscanf function reads data format in a text file or ASCII file.
- The fgetl and fgets functions read one line of the file at a time, where a newline character separates each line.
- The fread function reads data streams at the byte or bit level.
Example
Suppose the file 'myfile.txt' is saved in the current directory. This file contains data about rainfall for 3 months: June, July, and August in the year 2012.
The data in the myfile.txt file includes time, month, and rainfall measured at 5 locations. The header data is the month number, denoted as M:
The file will have the following format:
Follow the steps below to input data into the file and display the data:
- Open the file using the fopen function and get the file handle.
- Describe the data in the file using variables like '%' for strings, '%d' for integers, or '% f' for floating-point numbers.
To skip alphabetic characters in the file, include these characters in the format description. To skip a data field, use the asterisk (*) character in the variable.
For example, to read the header and return the unique value for M, you write:
M = fscanf(fid, '%*s %*s\n%*s %*s %*s %*s\nM=%d\n\n', 1);
- By default, fscanf reads data according to the format description until the function finds no match for the data or reaches the end of the file. In this example, a loop will be used to read 3 sets of data, and each time it will read 7 rows and 5 columns.
- Create a structure named 'mydata' in the workspace to store data read from the file. This structure consists of 3 fields: time, month, and the raindata array.
Create a script file and input the following code snippet:
Upon running the file, it will return the following result:
With the information about MATLAB - Data Input Techniques in MATLAB that Mytour just introduced above, hopefully you will learn more useful information about MATLAB. Additionally, readers can refer to other articles on Mytour such as how to add mathematical symbols in MATLAB to quickly get familiar with this software.
