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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With