Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab - usage of workspace variables

Tags:

matlab

I want to create a function function ExtremePoints = AnalyseData( ScanData ).

I want to be able to run the function without passing the argument ScanData, and in this situation I want to use a variable with the same name from Matlab Workspace.

Is this possible, to use inside the body of the function the variable ScanData which appear in workspace?

Or should I first save the content of the variable ScanData from workspace into a .mat file and then load that file in the body of the function?

like image 995
Simon Avatar asked Jul 27 '11 11:07

Simon


People also ask

How does MATLAB use workspace data?

To load saved variables from a MAT-file into your workspace, double-click the MAT-file in the Current Folder browser. To load a subset of variables from a MAT-file on the Home tab, in the Variable section, click Import Data. Select the MAT-file you want to load and click Open.

Can MATLAB function access workspace variables?

Nested functions can access and modify variables in the workspaces of the functions that contain them. All of the variables in nested functions or the functions that contain them must be explicitly defined.

How do I show variables in MATLAB workspace?

Command Window — To view the value of a variable in the Command Window, type the variable name. For the example, to see the value of a variable n , type n and press Enter. The Command Window displays the variable name and its value. To view all the variables in the current workspace, call the who function.


1 Answers

It is possible, perhaps not entirely recommended. Here's how:

function ExtremePoints = AnalyseData( ScanData )
if nargin == 0
    ScanData = evalin( 'base', 'ScanData' );
end
% do stuff

This pulls the value of ScanData from the base workspace if no input arguments are supplied (nargin == 0).

Use of eval and evalin is generally discouraged as it makes your code harder to understand and re-use.

like image 92
Edric Avatar answered Sep 23 '22 04:09

Edric