tags:

views:

510

answers:

2

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.

+3  A: 

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.

gnovice
This is not the case if you look ath the provided code. This is about the ability to place MATLAB code in separate "cells" to ease development.
kigurai
The provided code is somewhat unclear on this point. You can't place MATLAB code in separate cells of a cell array. You CAN place function handles in a cell array, like: fhArray = {@foo1, @foo2}.
gnovice
Unfortunately that's not what I'm trying to do but thanks for trying to help out anyway.
snth
I've fixed my answer to account for my misunderstanding of what you were asking. Sorry for the mixup.
gnovice
Thanks. Although this wasn't quite the answer I was hoping for this does get the job done and what I wanted is actually probably not possible.
snth
A: 

The way that I normally handle that is by using dbstop somewhere inside of the main function. Then you have access into all of the functions which the main function would normally have access to. If you're working with the ML editor, just use a breakpoint at the first call to foo.

Hope it helps.

Dan

Dan

related questions