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:
create a function

Subject: create a function

From: Pink Panther

Date: 11 Jan, 2011 16:08:07

Message: 1 of 6

hi,
I am trying to create a function to be called from other part of the code. it begins like this :

function createfigure(data,param,synthese,resultats)

%% convert time from excel format to decimal format
if data(1,1)>2500
    startd=dateve
etc.
etc.

this gives an error message on line "if data(1,1) ...", saying that data is not known while if I type data in the command window, data is here ok. what's happening ? thanks in advance

Subject: create a function

From: Oleg Komarov

Date: 11 Jan, 2011 16:31:06

Message: 2 of 6

"Pink Panther" wrote in message <ighv97$ge6$1@fred.mathworks.com>...
> hi,
> I am trying to create a function to be called from other part of the code. it begins like this :
>
> function createfigure(data,param,synthese,resultats)
>
> %% convert time from excel format to decimal format
> if data(1,1)>2500
> startd=dateve
> etc.
> etc.
>
> this gives an error message on line "if data(1,1) ...", saying that data is not known while if I type data in the command window, data is here ok. what's happening ? thanks in advance

You're not telling everything. I dont' get (obviously) any problem.
Can you post the whole error message please.

Oleg

Subject: create a function

From: Pink Panther

Date: 11 Jan, 2011 16:47:05

Message: 3 of 6

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <igi0ka$dt3$1@fred.mathworks.com>...
> "Pink Panther" wrote in message <ighv97$ge6$1@fred.mathworks.com>...
> > hi,
> > I am trying to create a function to be called from other part of the code. it begins like this :
> >
> > function createfigure(data,param,synthese,resultats)
> >
> > %% convert time from excel format to decimal format
> > if data(1,1)>2500
> > startd=dateve
> > etc.
> > etc.
> >
> > this gives an error message on line "if data(1,1) ...", saying that data is not known while if I type data in the command window, data is here ok. what's happening ? thanks in advance
>
> You're not telling everything. I dont' get (obviously) any problem.
> Can you post the whole error message please.
>
> Oleg

here is the code :
function createfigure(data, param, synthese, resultats)

%% convert time from excel format to decimal format
if data(1,1)>2500
    startd=datevec(data(1,1));
    startynum=datenum([startd(1) 1 1 0 0 0]);
    if startd(1)>15
        starty=1900+startd(1);
    else
        starty=2000+startd(1);
    end
    t(:,1)=starty*ones(length(data),1)+(data(:,1)-startynum)/365.25;
else
    t=data(:,1);
end

here is the error generated on these few lines of code :
"Matlab Editor"
"The selected cell cannot be evaluated beacuse it contains an invalid argument"

as the code aforementionned works perfectly well in another program I wrote, I assume the problem comes from the data itself.

Subject: create a function

From: Steven_Lord

Date: 11 Jan, 2011 18:38:26

Message: 4 of 6



"Pink Panther " <huguesrialan@gmail.com> wrote in message
news:ighv97$ge6$1@fred.mathworks.com...
> hi, I am trying to create a function to be called from other part of the
> code. it begins like this :
> function createfigure(data,param,synthese,resultats)
>
> %% convert time from excel format to decimal format
> if data(1,1)>2500
> startd=dateve
> etc.
> etc.
>
> this gives an error message on line "if data(1,1) ...", saying that data
> is not known while if I type data in the command window, data is here ok.
> what's happening ? thanks in advance

How are you calling this function? You need to call it with at least one
input argument. If you don't, then there will be no variable named data in
the workspace of the createfigure function and so MATLAB will (correctly)
error when you ask for its first element.

For a description of function workspaces, read:

http://www.mathworks.com/help/techdoc/matlab_prog/f7-38085.html

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: create a function

From: TideMan

Date: 11 Jan, 2011 19:45:12

Message: 5 of 6

On Jan 12, 5:47 am, "Pink Panther " <huguesria...@gmail.com> wrote:
> "Oleg Komarov" <oleg.komarovRemove.t...@hotmail.it> wrote in message <igi0ka$dt...@fred.mathworks.com>...
> > "Pink Panther" wrote in message <ighv97$ge...@fred.mathworks.com>...
> > > hi,
> > > I am trying to create a function to be called from other part of the code. it begins like this :
>
> > > function createfigure(data,param,synthese,resultats)
>
> > > %% convert time from excel format to decimal format
> > > if data(1,1)>2500
> > >     startd=dateve
> > > etc.
> > > etc.
>
> > > this gives an error message on line "if data(1,1) ...", saying that data is not known while if I type data in the command window, data is here ok. what's happening ? thanks in advance
>
> > You're not telling everything. I dont' get (obviously) any problem.
> > Can you post the whole error message please.
>
> > Oleg
>
> here is the code :
> function createfigure(data, param, synthese, resultats)
>
> %% convert time from excel format to decimal format
> if data(1,1)>2500
>     startd=datevec(data(1,1));
>     startynum=datenum([startd(1) 1 1 0 0 0]);
>     if startd(1)>15
>         starty=1900+startd(1);
>     else
>         starty=2000+startd(1);
>     end
>     t(:,1)=starty*ones(length(data),1)+(data(:,1)-startynum)/365.25;
> else
>     t=data(:,1);
> end
>
> here is the error  generated on these few lines of code :
> "Matlab Editor"
> "The selected cell cannot be evaluated beacuse it contains an invalid argument"
>
> as the code aforementionned works perfectly well in another program I wrote, I assume the problem comes from the data itself.

You're trying to run this from within the Editor by clicking Evaluate
Cell, but when you do this, it has no input arguments defined.
Evaluate Cell only works in a script, not a function.
You need to either: go to the Command Window and call it with
arguments; or write a script that calls the function, then execute
using Evaluate Cell.

Subject: create a function

From: Steven_Lord

Date: 11 Jan, 2011 22:03:52

Message: 6 of 6



"TideMan" <mulgor@gmail.com> wrote in message
news:68b8f89f-47fa-4513-9fc9-f5599d0468b7@i41g2000vbn.googlegroups.com...
> On Jan 12, 5:47 am, "Pink Panther " <huguesria...@gmail.com> wrote:
>> "Oleg Komarov" <oleg.komarovRemove.t...@hotmail.it> wrote in message
>> <igi0ka$dt...@fred.mathworks.com>...
>> > "Pink Panther" wrote in message <ighv97$ge...@fred.mathworks.com>...

*snip*

>> here is the error generated on these few lines of code :
>> "Matlab Editor"
>> "The selected cell cannot be evaluated beacuse it contains an invalid
>> argument"
>>
>> as the code aforementionned works perfectly well in another program I
>> wrote, I assume the problem comes from the data itself.
>
> You're trying to run this from within the Editor by clicking Evaluate
> Cell, but when you do this, it has no input arguments defined.
> Evaluate Cell only works in a script, not a function.
> You need to either: go to the Command Window and call it with
> arguments; or write a script that calls the function, then execute
> using Evaluate Cell.

Or use a run configuration.

http://www.mathworks.com/help/techdoc/matlab_env/brqxeeu-131.html#brqxeeu-134

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with 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