How to Read a .mat File in Matlab
MAT-Files
Viewing the Contents of a MAT-File
MAT-files are binary MATLAB format files that store workspace variables.
To see the variables in a MAT-file before loading the file into your workspace, click the file name in the Current Folder browser. Information nigh the variables appears in the Details Console.
Alternatively, use the control whos -file filename. This function returns the name, dimensions, size, and course of all variables in the specified MAT-file.
For example, view the contents of the demo file durer.mat:
whos -file durer.mat
MATLAB returns:
Name Size Bytes Class Attributes Ten 648x509 2638656 double explanation 2x28 112 char map 128x3 3072 double
The byte counts represent the number of bytes that the data occupies when loaded into the MATLAB workspace. Compressed data uses fewer bytes in a file than in the workspace. In Version 7 or college MAT-files, MATLAB compresses data. For more information, meet MAT-File Versions.
Loading a MAT-File
To import all variables from a MAT-file, utilize one of the post-obit options:
- In the Electric current Binder browser, double-click the proper noun of the file, or right-click the proper name of the file and select Open or Load.
Phone call the load part. For case, the following command loads all variables from the demo file durer.mat:
load('durer.mat')
To load variables into a construction assortment, specify an output variable for the load office:
durerStruct = load('durer.mat')
[alert]When yous import data into the MATLAB workspace, the new variables you create overwrite whatsoever existing variables in the workspace that have the aforementioned name.[/warning]
Loading Selected Variables
To select and load MAT-file variables interactively, use any of the post-obit options:
- Select File > Import Information.
- Click the Import data button in the Workspace browser toolbar.
- Drag variables from the Details Panel of the Current Folder browser to the Workspace browser.
Alternatively, utilize the load office. For example, load variables X and map from durer.mat:
load('durer.mat', 'X', 'map')
To load variables with names that match a specific pattern, utilize one of the following options:
Include the wildcard character (*). For example, load all variables that start with str from a file named strinfo.mat:
load('strinfo.mat', 'str*')
Utilize the -regexp pick, which matches variables to regular expressions. For example, load all variables that beginning with Mon, Tue, or Wed from a hypothetical file named myfile.mat:
load('myfile.mat', '-regexp', '^Mon|^Tue|^Midweek')
[important]For more information, see Regular Expressions in the MATLAB Programming Fundamentals documentation.[/important]
Troubleshooting: Loading Variables within a Role
If you ascertain a function that loads data from a MAT-file, and find that MATLAB does not return the expected results, check whether any variables in the MAT-file share the aforementioned name as a MATLAB part. Mutual variable names that conflict with function names include i, j, style, char, size, or path. To determine whether a particular proper noun is associated with a MATLAB part, employ the which role.
[tip]For example, consider a MAT-file with variables meridian, width, and length. If you load these variables using a function such every bit findVolume,[/tip]
part vol = findVolume(myfile) load(myfile); vol = summit * width * length;
MATLAB interprets the reference to length equally a call to the length function, and returns an error:
??? Error using ==> length Not enough input arguments.
To avert confusion, when defining your part, choose i (or more) of the following approaches:
Load into a structure array. For instance, define the findVolume function equally follows:
role vol = findVolume(myfile) dims = load(myfile); vol = dims.top * dims.width * dims.length;
- Explicitly include the names of variables in the phone call to load, as described in Loading Selected Variables.
- Initialize variables within the function before calling load.
Any of these methods allow you to override the function name with the variable name inside your function. For more information, run into Potential Conflict with Function Names in the MATLAB Programming Fundamentals documentation.
Source: https://www.matlabtutorials.com/importing-mat-files/
0 Response to "How to Read a .mat File in Matlab"
Post a Comment