Open In App

Share Data Between Workspaces in MATLAB

Improve
Improve
Like Article
Like
Save
Share
Report

Workspaces in MATLAB are the scopes of a particular code entity such as a function. In general, data cannot be shared between two workspaces or, one can say that a data variable created in one workspace cannot be accessed by some other workspace, without additional support. In this article, we shall discuss this very additional support or ways in which data can be shared by different workspaces. For implementation purposes, this article uses functions to represent different workspaces. However, different scripts or live scripts also are types of different workspaces. 

Let us now understand the ways to share data/variables among workspaces in MATLAB.

Use of Function Arguments:

As it is a standard practice to use input and output arguments in MATLAB functions, they are also the best way to share data between two different functions. Following is an example of the same.

Example 1:

Matlab




% MATLAB Code
res=combination(9,7)
 
% Defining two functions to
% calculate the binomial coefficients
function out=combination(n, k)
    out = factorial(n)/denom(n,k);
end
 
function out = denom(n,k)
    out = factorial(n-k)*factorial(k);
end


Output:

In the above code, we use two functions to calculate the combination of two numbers. The second function calculates the denominator of the combination term and stores it as the output of the denom function. This is the only way of doing the above task in MATLAB.

 

 

Use of Nested Functions:

Nested functions allow the child functions to access their parent functions’ variables as long as they are declared in the parent function. Let us see the same through the following example.

Example 2:

Matlab




% MATLAB Code
sqr
 
% Function
function sqr
    num = 3;
    work_function
    
   % Child function to calculate and
   % display the square of the num
    function work_function
        num=num*num;
        dip(num)
    end
end


Output:

In the above function, we declare a variable num and then calculate and display its square within a child function. See the output below.

 

Using Global Keyword:

When a variable is initialized using the global keyword in any workspace, it becomes accessible to all functions/workspaces in the current session. See the following example.

Example 3:

Matlab




% MATLAB code for setting global g to 9.8
g=9.8;
my_w = weight(20);
fprintf("Weight at earth =")
disp(my_w)
 
% Changing global g to 1.625
g=1.625;
my_w = weight(20);
fprintf("Weight at moon =")
disp(my_w)
 
% Function
function out = weight(mass)
    global g;
    out = mass*g;
end


Output:

In the above code, we created a function to calculate the weight of a person on different values of acceleration due to gravity. We change the value of g to 9.8 and 1.625 respectively and then calculate the weight respectively. The output would be,

 

Using PERSISTENT Variables:

Persistent variables in MATLAB are similar to static variables in other programming languages. These variables are limited to the function’s workspace they are defined in, and their values cannot be altered by other functions or function calls. MATLAB initializes a persistent variable as an empty matrix. The syntax would be:

persistent variable_name

Now, we will see the usage of these persistent variables with an example.

Example 4:

Matlab




% MATLAB function
function geeks
 
% Defining the persistent variable
  persistent call_count;       
   
% Defining an local variable
  count = 0;                       
  if(isempty(call_count))
      call_count =0;
  end
   
  % Incrementing in both variables
  call_count = call_count+ 1;       
  count = count+1;
   
  % Displaying the values of both variables
  fprintf("Number of Function calls =")
  disp(call_count)
  fprintf("Normal count variable =")
  disp(count)
end


Output:

The above function defines a persistent variable that will count the number of times a function is called. In the function, we initialize the call_count variable when it is empty and initialize another variable(local) to see the difference between an ordinary local variable and a persistent variable. Then, incrementing both variables by 1. The following output shows the difference in the former two.

 

As it can be seen that the local variable count is initialized every time the geek’s function is called however, the persistent variable call_count persists in the memory and retains its values in all workspaces.

Evaluating in another workspace

MATLAB provides the evalin () function to exchange variables from different workspaces. The syntax of the same is 

new_var = evalin(workspace, expression)

Where, the workspace is the workspace you are requesting the expression from. In the following example, we understand the same.

Example 5:

Matlab




my_var = magic(3);
copy_mat
 
function copy_mat
    % Getting the my_var expression from script
    % into the function without passing arguments
    new_mat = evalin("caller",'my_var');
    disp(new_mat)
end


Here, we get the value of my_var in the copy_mat function without the requirement of any input-output variables.

Output:

 



Last Updated : 06 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads