Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
function call error

Subject: function call error

From: BP

Date: 25 Jun, 2012 07:21:07

Message: 1 of 4

I tried to write matlab code in a modularized way, but I am not sure if it's correct, as compiler return error when I did the below:

main.m is the script which triggers multiple trial run of experiment.
algo.m contains the procedure to execute each experiment.
common.m contains utility function for file reading etc.

When I compiled at main.m, error returns
"Undefined function 'experiment' for input arguments of type 'char'."

May I know how to overcome the error, and is this the correct way to write modular matlab script?


**************
main.m which contains the below lines
----------------
R = experiment('t1_run.txt');
R = experiment('t2_run.txt');
R = experiment('t3_run.txt');


**************
algo.m which run each experiment
----------------
function ret = experiment(filepath)

% read data from file
D = read_data(filepath, '%.3f');

% run experiment step 1
% run experiment step 2
% run experiment step n

ret = 1;
return
----------------

**************
common.m which read files
----------------
function D = read_data(path, format)
    fid = fopen(path,'r');
    D = textscan(fid, format);
    fclose(fid);
return
----------------

Subject: function call error

From: Nasser M. Abbasi

Date: 25 Jun, 2012 07:49:15

Message: 2 of 4

On 6/25/2012 2:21 AM, BP wrote:

>
> **************
> algo.m which run each experiment
> ----------------
> function ret = experiment(filepath)

try to change the file name to experiment.m

>
> **************
> common.m which read files
> ----------------
> function D = read_data(path, format)

try to change the file name to read_data.m

File name should match the first function name in the file.

--Nasser

Subject: function call error

From: BP

Date: 25 Jun, 2012 09:35:07

Message: 3 of 4

"Nasser M. Abbasi" <nma@12000.org> wrote in message
> File name should match the first function name in the file.
>
> --Nasser

What if i have two functions in the same file?

Let say in plot_cdf.m, two functions were defined as below. But compiler returns run-time error "Undefined function 'plot_ecdf' for input arguments of type 'double'."

--------------------
function [ycdf,xcdf] = plot_cdf(A, LineSpec)
     [ycdf,xcdf] = cdfcalc(A);
end

function plot_ecdf(A)
        % Calculate and plot empirical cdf and confidence bounds
        [f,x,flo,fup] = ecdf(A);
end

Subject: function call error

From: Steven_Lord

Date: 25 Jun, 2012 13:27:32

Message: 4 of 4



"BP " <syseeker@yahoo.com> wrote in message
news:js9bcb$kho$1@newscl01ah.mathworks.com...
> "Nasser M. Abbasi" <nma@12000.org> wrote in message
>> File name should match the first function name in the file.
>>
>> --Nasser
>
> What if i have two functions in the same file?

The main (first) function in the file should have the same name as the name
of the file; if they are different, then that function will be known by the
FILE's name. Since the subfunctions in the file are not directly accessible
outside the file, you can name them whatever you want (as long as it's a
valid MATLAB identifier, of course.)

> Let say in plot_cdf.m, two functions were defined as below. But compiler
> returns run-time error "Undefined function 'plot_ecdf' for input arguments
> of type 'double'."

That's correct. Subfunctions are not "visible" outside the file in which
they are defined.

http://www.mathworks.com/help/techdoc/matlab_prog/f4-70666.html

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us