File Storage
MATLAB supports saving the work area. You can save the variables in the workspace or workspace as files for re-import as needed. You can save the workspace either through the menu or through the command window.
1. Save the entire Workspace
Select save workspace as… In the File menu... Command, or click Save in the toolbar of the workspace browser to save the variables in the workspace as mat files.
2. Save the variables in the Workspace
In the workspace browser, right-click the variable name to be saved and select Save ..., Save the variable as a mat file.
3. Use the Save command to save
This command can save the workspace or any specified file in the workspace. The Calling format of this command is as follows:
●Save: Save all variables in the workspace to the files in the current workspace. The file name is Matlab. the mat and mat files can be imported to the workspace again using the load function, which can be imported by different machines or even called by other programs.
● Save ('filename'): saves all variables in the workspace as files, and the file name is specified by filename. If filename contains a path, the file is saved in the corresponding directory; otherwise, the default path is the current path.
●Save ('filename', 'var1', 'var2',...): Save the specified variable in the file specified by filename.
● Save ('filename', '-struct','s '): stores all fields in struct s as independent variables.
● Save ('filename', '-struct','s ', 'f1', 'F2',...): Save the specified variable in struct s.
● Save ('-Regexp', expr1, expr2,...): Use a regular expression to specify the conditions to be met for the variable to be saved.
● Save ('..., 'format'): Specifies the format of the file to be saved. The format can be mat or ASCII.
13.1.2 Data Import
Data Import in MATLAB is usually implemented by the function load. The usage of this function is as follows:
● Load: If the MATLAB. Mat file exists, import all variables in MATLAB. Mat. If not, an error is returned.
● Load filename: import all the variables in filename to the workspace.
● Load filename x y z...: import the variables X, Y, and Z in filename to the workspace. If it is a mat file, you can use the wildcard "*" when specifying the variables.
● Load filename-Regexp expr1 expr2...: Use a regular expression to specify the variable to be imported.
● Load-ASCII filename: whether or not the input file name contains an extension, it is imported in ASCII format. If the specified file is not a numeric text, an error is returned.
● Load-mat filename: whether or not the input file name contains an extension, it is imported in mat format. If the specified file is not a mat file, an error is returned.
Example 13-1 import the variables in the file MATLAB. map to the workspace.
First, run the Whos-file command to view the content in the file:
> Whos-file MATLAB. Mat
Name size bytes class
A 2x3 48 double Array
I _q 415x552x3 687240 uint8 Array
Ans 1x3 24 Double Array
Num_of_cluster 1x1 8 double Array
Grand total is 687250 Elements Using 687320 bytes
Import the variables in the file to the Workspace:
> Load MATLAB. Mat
After the command is executed, you can see these variables in the workspace browser, as shown in 13-1.
Figure 13-1 workspace view after variables are imported
Then you can access these variables.
> Num_of_cluster
Num_of_cluster =
3
In Matlab, another common function for data import is importdata. The usage of this function is as follows:
● Importdata ('filename'), which imports data from filename to the workspace;
● A = importdata ('filename'), import the data in filename to the workspace, and save it as variable;
● Importdata ('filename', 'delimiter'), which imports data from filename to the workspace and uses the symbols specified by delimiter as separators;
Example 13-2 import data from a file.
> Imported_data = importdata ('matlab. M ')
Imported_data =
Ans: [1.1813 1.0928 1.6534]
A: [2x3 double]
I _q: [415x552x3 uint8]
Num_of_cluster: 3
Different from the load function, importdata imports the data in the file into the work zone as a struct.
13.1.3 open a file
In Matlab, you can use the open command to open files in various formats. MATLAB automatically selects the appropriate editor based on the file extension.
Note that open ('filename. mat ') and load ('filename. mat '), the former uses filename. MAT is opened in the workspace as a struct. The latter imports the variables in the file into the workspace. If you need to access the content, you need to use different formats.
Example 13-3 Comparison between open and load.
> Clear
> A = magic (3 );
> B = rand (3 );
> Save
Saving to: MATLAB. Mat
> Clear
> Load ('matlab. M ')
>
A =
8 1 6
3 5 7
4 9 2
> B
B =
0.9501 0.4860 0.4565
0.2311 0.8913 0.0185
0.6068 0.7621 0.8214
> Clear
> Open ('matlab. M ')
Ans =
A: [3x3 double]
B: [3x3 double]
> Struc1 = ans;
> Struc1.a
Ans =
8 1 6
3 5 7
4 9 2
> Struc1. B
Ans =
0.9501 0.4860 0.4565
0.2311 0.8913 0.0185
0.6068 0.7621 0.8214