Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to call a subfunction while in cell mode in matlab?

Tags:

matlab

Say I had the following code:

% Cellmode_subfunction_test.m
%% Cell 1
foo(1);

%% Cell 2
foo(2);

%% Definition of the foo subfunction
function foo(num)
disp(['num=' num2str(num)]);

How can test cell 1 and cell 2 with the subfunction defined at the end?

Edit: Basically each of the cells in this example perform some lengthy calculations so I'd like to test and debug them separately. I'm using subfunctions to abstract out and reuse common functionality and since so far this functionality is only used in this particular application I don't really want to place foo in a separate m-file.

Edit(2): I just remembered that I vaguely recall cell mode only working in matlab scripts and not in function m-files and that subfunctions and nested functions are not allowed in such scripts. Thus what I'm asking for is probably not possible.

Although the anonymous function solution given below is perhaps somewhat restrictive as it only allows single expression functions, it did in fact suffice for what I wished to do and hence I've accepted it as a solution to my problem.

like image 823
snth Avatar asked Feb 02 '09 15:02

snth


1 Answers

CORRECTION:

I misunderstood your use of the word CELL. My apologies. It appears you simply want to define a function at the command line without saving it to a .m file. For this, you can use anonymous functions:

foo = @(num) disp(['num=' num2str(num)]);

Then you can use "foo" as you would any other function.

like image 54
gnovice Avatar answered Oct 12 '22 09:10

gnovice