A blog to help electrical engineers and students to learn electrical engineering topics. Basic Electrical Engineering, Power Systems, Electrical Machines, tec.. Now special course on MATLAB for Beginners.



Saturday, June 25, 2016

The load and save Functions

Load( ) - Load data from MAT-file into workspace

The variables in the workspace are saved by default as matlab.mat file. The load command loads these variables to the workspace. If the variables are stored in a specific file, use the syntax:
S = load(‘Filename’).
The command loads the variables from a Filename.mat file into a structure array named S. If the file is not .mat file, it will be considered as an ASCII file and load data into a double-precision array. ASCII files must contain a rectangular table of numbers, with an equal number of elements in each row.  The file delimiter can be a blank, comma, semicolon, or tab. 
If the file has multiple variables, we can load some specific variables from the file using,
S = load(‘filename’, ‘variable1’, ‘varible2’, …)
Use the '*' wildcard to match patterns.  For example, load('A*') loads all variables that start with A.
Consider vasriables a,b and c saved to default matalab.mat and variables x, y and z are save to test.mat file.
>> load
Loading from: matlab.mat
Load the variables a b and c to the workspace.
>> load test.mat
Load the varibles x y and z to the workspace.
>> S = load('test.mat')
S =
    x: [1 2 3 4 5]
    y: [1 2 3 4 5 6 7 8 9 10]
    z: 'ElectricalEngineering'
The command load the values x y and z to the structure S.
>> P = load('test.mat', 'x')
P =
    x: [1 2 3 4 5]
Load only value of x to the structure P.

save( ) - Save workspace variables to file

The save command is used to save all the variables in the work space to the matlab.mat file. The variables can be asved to some specific file using,
The command stores all the variables in the current workspace in a file named filename.mat. If the filename does not include a full path, MATLAB create the file in the current folder.
We can save selected variables only using,
save(‘filename’, ‘veriable1’, ‘variable2’, …).
If we use the save command in the command window, the bracket and the single quotations are not required. The filename can be entered directly after the save with a space in between. Separate input variables with spaces instead of commas.
There are more optional arguments to define the type of the file and the way of storing the variables.
'-struct' – is used to save the fields of the specified structure as individual variables. Specific fields of the structure can be selected by adding the field names after the structure name.
save(‘filename’, '-struct', ‘Structname’, ‘Fieldname’)
'-mat' or '-ascii' – used to specify the format of the file. By default the save will use .mat format. For more options on format, refer Matlab help.
'-append' – used to adds new variables to an existing file,    adds data to the end of the file.
Default save command save all the variables to the Matlab.mat file
>> save
Saving to: matlab.mat
>> open('matlab.mat')
ans =
    x: [1 2 3 4 5]
    y: [1 2 3 4 5 6 7 8 9 10]
    z: 'ElectricalEngineering'
    S: [1x1 struct]
    P: [1x1 struct]
 Save only variables x and y to xydat.mat
>> save('xydata.mat', 'x', 'y')
>> open('xydata.mat')
ans =
    x: [1 2 3 4 5]
    y: [1 2 3 4 5 6 7 8 9 10]
Save the fields of a structure as individual variables:
>> save('newstruct.mat', '-struct', 'S')
>> open('newstruct.mat')
ans =
    x: [1 2 3 4 5]
    y: [1 2 3 4 5 6 7 8 9 10]

    z: 'ElectricalEngineering'
submit to reddit