Updated June 22, 2023
Introduction to Matlab Save Variable
There are two operations in Matlab Save Variable which operate on file variables. One is the saved function, and the other is the load function. The save function saves the current working variable to a specific file. And load function is used to load the current working variable to a specific file. We must consider the workspace column to deal with the save and load functions. The workspace shows the total variables list along with the values in 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 the 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 a 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 must be loaded in a file. ( load file1 ), Here the load is a function, and file1 is the file’s name.
- Load file name variables name: This syntax is used when limited or specific variables must be loaded 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 using the ‘save workspace’ tab, and another is using the ‘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 must create one file to save the variables. By default, it will save all the variables in the file.
Steps to use the save function with default variables:
- Declare and assign variables ( v1, v2, v3 )
- Click on the save workspace tab
- Or use the save command ( save filename: save file 1 )
Steps to use the save function with variables:
- Declare and assign variables ( v1, v2, v3 )
- Click on the save workspace tab
- Or use the 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 the load function and mention variable names ( load file 1 v1,v2)
Examples of Matlab Save Variable
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
Consider the 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 essential functions in Matlab to handle the file variables. With the help of the 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 to save variable works in Matlab, and examples. You can also go through our other related articles to learn more–