开发者

Matlab - usage of workspace variables

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开发者_JAVA技巧 file and then load that file in the body of the 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜