Introduction to Matlab Save Variable
There are two operations in Matlab Save Variable which operate on file variables. One is saved function and the other is load function. The save function is used to save the current working variable to a specific file. And load function is used to load the current working variable to a specific file. To deal with save function and load function always we need to consider the workspace column. The workspace shows the total variables list along with the values in the form of excel sheets. Every time workspace automatically gets an update as we execute the program.
Syntax:
- Save filename: This syntax is used when all the current variables need to be stored in a specific file. (save file1), here save is function and file1 is the name of the file.
- Save filename variable names: This syntax is used when limited or specific variables need to be stored in a file. ( save file1 v1, v2) , here save is function and file1 is the name of the file and v1,v2 are variables.
- Load file name: This syntax is used when all the current variables from the existing file need to be load in a file. ( load file1 ), Here the load is function and file1 is the name of the file.
- Load file name variables name: This syntax is used when limited or specific variables need to be load in the file. (load file1 v1,v2) , here load is function and file1 is the name of the file and v1,v2 are variables
How does Save Variable work in Matlab?
There are two ways to save variables in Matlab. One way is by using the ‘save workspace’ tab and another way is by using ‘save’ command. To save the variables in the file first method is very easy in which first we need to declare variables and then need to assign the variables. After assigning the values we need to create one file where we can save the variables. By default, it will save all the variables in the file.
Steps to use save function with default variables:
- Declare and assign variables ( v1, v2, v3 )
- Click on save workspace tab
- Or use save command ( save filename: save file 1 )
Steps to use save function with variables:
- Declare and assign variables ( v1, v2, v3 )
- Click on save workspace tab
- Or use save command and mention variable names ( save filename variable 1, variable 2: save file 1 v1, v2 )
Steps to load functions with default variables:
- Clear workspace
- Use the load function ( load file 1 )
Steps to load functions with variables:
- Clear workspace
- Use load function and mention variable names ( load file 1 v1,v2)
Examples of Matlab Save Variable
The examples of the following are given below:
Example #1
Let us consider one example with variables v1, v2 and v3 here values of v1 are 7, v2 is 4 and v3 is 9.
Code:
>> v1 = 7
>> v2 = 4
>> v3 = 9
> save file 1
Output:
Code:
> clear
> load file1
Output:
Code:
>> clear
>> load file1 v1 v2
>>
Output:
Example #2
Let us consider second example with variables var1, var 2, var 3 and var 4. Here var 1 and var 2 are input variables and var3 and var 4 are output variables.
Code:
>> clear
>> var1 = 30
>> var2 = 65
>> var3 = var1 + var2
>> var4 = var1 - var2
>> save file2
Output:
Code:
>> clear
>> load file2
Output:
Code:
>> clear
>> load file2 var3 var4
>>
Output:
Conclusion
The function ‘save’ and ‘load’ are very important functions in Matlab to handle the variables in files. With the help of save function, we store the variable values and data permanently in the machine, so that we can use the values anytime in any program throughout the programming.
Recommended Articles
This is a guide to Matlab Save Variable. Here we discuss the introduction, how does save variable work in Matlab along with the examples. You can also go through our other related articles to learn more–
- Natural Log in Matlab
- Arrays in Matlab
- Matlab colorbar Label | How to Work? | Examples
- Matlab Count | Examples | How to Work?
- Examples of fminsearch in Matlab
3 Online Courses | 1 Hands-on Project | 8+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses