Thread Subject:
PUBLISH on a function with several arguments

Subject: PUBLISH on a function with several arguments

From: Herve

Date: 26 Oct, 2009 11:22:01

Message: 1 of 9

I am writing in reference to this question : "How do I use PUBLISH on a function if I want to also pass in an argument to that function in MATLAB 7.7 (R2008b)? "

that I have been answered here :

http://www.mathworks.com/support/solutions/en/data/1-97WVV6/?solution=1-97WVV6

However, I can get it work when I have more than one arguments to my function. Answer says that "If your function has more than one input variable, one potential solution is to use VARARGIN to handle multiple input arguments or to encapsulate the input args in a cell array to be read in as the third argument".

Ok, but in pratice, how do you write your code ?
Can someone help me ?

If you use the two M-functions given at the bottom of the webpage (link given below) and if you change the foo.m function as :

function foo(a,b)
% a is a vector
% b is for instance a color : 'r'
plot(a,b);
end

then how do you modify the function mypublish.m ?

Best regards,

Herv

Subject: PUBLISH on a function with several arguments

From: Herve

Date: 26 Oct, 2009 11:31:02

Message: 2 of 9


> However, I can get it work when I have more than one arguments to my function.

Sorry... One must read : I CANNOT get it work when I have more than one arguments to my function.

Subject: PUBLISH on a function with several arguments

From: Steven Lord

Date: 26 Oct, 2009 13:42:24

Message: 3 of 9


"Herve " <michaud_rene@yahoo.fr> wrote in message
news:hc40op$5iv$1@fred.mathworks.com...
>I am writing in reference to this question : "How do I use PUBLISH on a
>function if I want to also pass in an argument to that function in MATLAB
>7.7 (R2008b)? "
>
> that I have been answered here :
>
> http://www.mathworks.com/support/solutions/en/data/1-97WVV6/?solution=1-97WVV6
>
> However, I can get it work when I have more than one arguments to my
> function. Answer says that "If your function has more than one input
> variable, one potential solution is to use VARARGIN to handle multiple
> input arguments or to encapsulate the input args in a cell array to be
> read in as the third argument".
>
> Ok, but in pratice, how do you write your code ?
> Can someone help me ?

What version of MATLAB are you using? If you're using release R2008a or
later, use a publish configuration.

http://www.mathworks.com/access/helpdesk/help/techdoc/rn/brga7wp-1.html#brg9eej-1

Alternately, if you want to do this programmatically, use the codeToEvaluate
option described in HELP PUBLISH.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: PUBLISH on a function with several arguments

From: Herve

Date: 26 Oct, 2009 14:35:18

Message: 4 of 9

Hi Steven,

Thanks for your answer :-)

yes, I want my documents to be generated automatically and therefore I want to use the PUBLISH function.

Yes, I know that I have to use the codeToEvaluate option. But how to use this option when you have two or more input arguments ?

Here is the exemple given by Mathworks...

If you have a function foo

%%%%%%%%
function foo(a)
plot(a);
end
%%%%%%%%%

then to publish on this function, you have to create the following mypublish function

%%%%%%%%
function mypublish(fun,funcall,inputs)

opts.codeToEvaluate=[
    inputs, char(10),...
    funcall,char(10)];

publish(fun,opts)
%%%%%%%%

and then call the following command :

mypublish('foo','foo(a)','a=1:10')


***********************************************

My question is :

How do you modify the function mypublish when the function foo is

%%%%%%%%
function foo(a,b)
plot(a,b);
end
%%%%%%%%%

I hope that I have been clear enough now...

Herv

Subject: PUBLISH on a function with several arguments

From: Steven Lord

Date: 27 Oct, 2009 14:09:50

Message: 5 of 9


"Herve " <michaud_rene@yahoo.fr> wrote in message
news:hc4c36$hj8$1@fred.mathworks.com...
> Hi Steven,
>
> Thanks for your answer :-)
>
> yes, I want my documents to be generated automatically and therefore I
> want to use the PUBLISH function.
>
> Yes, I know that I have to use the codeToEvaluate option. But how to use
> this option when you have two or more input arguments ?
>
> Here is the exemple given by Mathworks...
>
> If you have a function foo
>
> %%%%%%%%
> function foo(a)
> plot(a);
> end
> %%%%%%%%%
>
> then to publish on this function, you have to create the following
> mypublish function
>
> %%%%%%%%
> function mypublish(fun,funcall,inputs)
>
> opts.codeToEvaluate=[
> inputs, char(10),...
> funcall,char(10)];
>
> publish(fun,opts)
> %%%%%%%%
>
> and then call the following command :
>
> mypublish('foo','foo(a)','a=1:10')
>
>
> ***********************************************
>
> My question is :
>
> How do you modify the function mypublish when the function foo is
>
> %%%%%%%%
> function foo(a,b)
> plot(a,b);
> end
> %%%%%%%%%
>
> I hope that I have been clear enough now...

publish('foo.m', struct('codeToEvaluate', 'foo(1:10, (1:10).^2)',
'showCode', false))

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: PUBLISH on a function with several arguments

From: Herve

Date: 4 Nov, 2009 13:13:03

Message: 6 of 9

Thank you Steve for your answer.
That saves me a lot of time...

Subject: PUBLISH on a function with several arguments

From: Herve

Date: 5 Nov, 2009 07:08:01

Message: 7 of 9

Something tricky !

Let's continue with the same function :

%%%%%%%%
function foo(a,b)
plot(a,b);
end
%%%%%%%%%

1) If I use directly the command window, and enter the following commands, it works.

>> a = 1:10;
>> b = 'r';
>> publish('foo',struct('codeToEvaluate','foo(a,b)','showCode',false))

2) However, if I create another function

%%%%%%%%
function call_foo
a = 1:10;
b = 'r';
publish('foo',struct('codeToEvaluate','foo(a,b)','showCode',false))
end
%%%%%%%%

and then run this function, I get an error message :

>> call_foo
Error using ==> evalin
Undefined function or variable 'a'.


WHY ?

H.

Subject: PUBLISH on a function with several arguments

From: Steven Lord

Date: 5 Nov, 2009 15:43:49

Message: 8 of 9


"Herve " <michaud_rene@yahoo.fr> wrote in message
news:hcttkh$580$1@fred.mathworks.com...
> Something tricky !
>
> Let's continue with the same function :
>
> %%%%%%%%
> function foo(a,b)
> plot(a,b);
> end
> %%%%%%%%%
>
> 1) If I use directly the command window, and enter the following commands,
> it works.
>
>>> a = 1:10;
>>> b = 'r';
>>> publish('foo',struct('codeToEvaluate','foo(a,b)','showCode',false))
>
> 2) However, if I create another function
>
> %%%%%%%%
> function call_foo
> a = 1:10;
> b = 'r';
> publish('foo',struct('codeToEvaluate','foo(a,b)','showCode',false))
> end
> %%%%%%%%
>
> and then run this function, I get an error message :
>
>>> call_foo
> Error using ==> evalin
> Undefined function or variable 'a'.
>
>
> WHY ?

I believe when you specify codeToEvaluate, it evaluates that code in the
base workspace, but I'm not 100% certain on that. I recommend you try to
ASSIGNIN the variables into the base workspace and try the code again. If
that works, you have a workaround but you might want to contact Technical
Support and describe your use case for this situation so they can capture it
as an enhancement request for publishing.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: PUBLISH on a function with several arguments

From: Herve

Date: 6 Nov, 2009 07:22:02

Message: 9 of 9


Excellent. That works now thanks to ASSIGNIN.

%%%%%%%%
function call_foo
 a = 1:10;
 b = 'r';
assignin('base','a',a)
assignin('base','b',b)
publish('foo',struct('codeToEvaluate','foo(a,b)','showCode',false))
end
%%%%%%%%

Thanks Steve.
I'll contact the support team about this issue.

H.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
publish Herve 26 Oct, 2009 07:24:06
rssFeed for this Thread

Contact us