Categories
2. After the Advance

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:

  1. Declare and assign variables ( v1, v2, v3 )
  2. Click on save workspace tab
  3. Or use save command ( save filename: save file 1 )

Steps to use save function with variables:

  1. Declare and assign variables ( v1, v2, v3 )
  2. Click on save workspace tab
  3. 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:

  1. Clear workspace
  2. Use the load function ( load file 1 )

Steps to load functions with variables:

  1. Clear workspace
  2. 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:

matlab save variable

Code:

> clear
> load file1

Output:

matlab save variable

Code:

>> clear
>> load file1 v1 v2
>>

Output:

matlab save variable

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:

matlab save variable

Code:

>> clear
>> load file2

Output:

load file2

Code:

>> clear
>> load file2 var3 var4
>>

Output:

load file2 var3 var4

Leave a Reply

Your email address will not be published. Required fields are marked *