matlab mat文件怎样读取和怎样调用

2025-04-08 09:54:58
推荐回答(1个)
回答1:

用命令load
句法有以下几种
load('filename')
load('filename', 'X', 'Y', 'Z')
load('filename', '-regexp', exprlist)
load('-mat', 'filename')
load('-ascii', 'filename')
S = load(...)
load filename -regexp expr1 expr2 ...

举例:
Example 1 -- Loading From a Binary MAT-fileTo see what is in the MAT-file prior to loading it, use whos -file: whos -file mydata.mat
Name Size Bytes Class

javArray 10x1 java.lang.Double[][]
spArray 5x5 84 double array (sparse)
strArray 2x5 678 cell array
x 3x2x2 96 double array
y 4x5 1230 cell array
Clear the workspace and load it from MAT-file mydata.mat: clear
load mydata

whos
Name Size Bytes Class

javArray 10x1 java.lang.Double[][]
spArray 5x5 84 double array (sparse)
strArray 2x5 678 cell array
x 3x2x2 96 double array
y 4x5 1230 cell array
Example 2 -- Loading From an ASCII File Create several 4-columnn matrices and save them to an ASCII file: a = magic(4); b = ones(2, 4) * -5.7; c = [8 6 4 2];
save -ascii mydata.dat
Clear the workspace and load it from the file mydata.dat. If the filename has an extension other than .mat, MATLAB assumes that it is ASCII: clear
load mydata.dat
MATLAB loads all data from the ASCII file, merges it into a single matrix, and assigns the matrix to a variable named after the filename: mydata
mydata =
16.0000 2.0000 3.0000 13.0000
5.0000 11.0000 10.0000 8.0000
9.0000 7.0000 6.0000 12.0000
4.0000 14.0000 15.0000 1.0000
-5.7000 -5.7000 -5.7000 -5.7000
-5.7000 -5.7000 -5.7000 -5.7000
8.0000 6.0000 4.0000 2.0000
Example 3 -- Using Regular ExpressionsUsing regular expressions, load from MAT-file mydata.mat those variables with names that begin with Mon, Tue, or Wed: load('mydata', '-regexp', '^Mon|^Tue|^Wed');
Here is another way of doing the same thing. In this case, there are three separate expression arguments: load('mydata', '-regexp', '^Mon', '^Tue', '^Wed');