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:
script inside function

Subject: script inside function

From: mashor housh

Date: 23 Nov, 2010 18:34:05

Message: 1 of 5

I have script file which contains definition of parametes e.g. cd=2;

when I use it inside a function e.g. :

function []=abc()
parameters
cdes=cd;
end

the value of cdes is a chars of the current folder i.e. the current folder function 'cd' is recognized by matlab insted of the variable defined in the the script file 'paramters'.
However, this problem does not occur if I use scrip inside script e.g. Mfiles contains:

parameters
cdes=cd

why this happened? and where I can read about this in matlab help?

Thank you,
Mashor

Subject: script inside function

From: Sean de

Date: 23 Nov, 2010 18:59:04

Message: 2 of 5

"mashor housh" <mashor@technion.ac.il> wrote in message <ich1et$c8v$1@fred.mathworks.com>...
> I have script file which contains definition of parametes e.g. cd=2;
>
> when I use it inside a function e.g. :
>
> function []=abc()
> parameters
> cdes=cd;
> end
>
> the value of cdes is a chars of the current folder i.e. the current folder function 'cd' is recognized by matlab insted of the variable defined in the the script file 'paramters'.
> However, this problem does not occur if I use scrip inside script e.g. Mfiles contains:
>
> parameters
> cdes=cd
>
> why this happened? and where I can read about this in matlab help?
>
> Thank you,
> Mashor

parameters has returned cd = to something. Before it was used by cdes it was assigned, i.e.
cd = 2;

The important answer is DON'T overwrite built-in/stock functions. It's bad practice and can be very dangerous.

Subject: script inside function

From: Rax

Date: 14 Jan, 2011 18:22:21

Message: 3 of 5

"mashor housh" <mashor@technion.ac.il> wrote in message <ich1et$c8v$1@fred.mathworks.com>...
> I have script file which contains definition of parametes e.g. cd=2;
>
> when I use it inside a function e.g. :
>
> function []=abc()
> parameters
> cdes=cd;
> end
>
> the value of cdes is a chars of the current folder i.e. the current folder function 'cd' is recognized by matlab insted of the variable defined in the the script file 'paramters'.
> However, this problem does not occur if I use scrip inside script e.g. Mfiles contains:
>
> parameters
> cdes=cd
>
> why this happened? and where I can read about this in matlab help?
>
> Thank you,
> Mashor


Aha! I'm looking at a somewhat relater problem. I've written a script to plot triplet data and I would like to be able to specify a director to which an .eps of the generated graph will be saved, but cannot find a means of passing the directory name from the matlab command line through to the 'command line in the script' (is there an unambiguous terminology used to distinguish between these two?). Below is the relevant part of the script - any help would be appreciated.

%%%%%%%%%
function triplet_plot (data,fmt, xlb, ylb, tit, dir, flnm)

x = data(:,1)';
y = data(:,2)';
e = data(:,3)';

errorbar(x,y,e,fmt)
xlabel(xlb);
ylabel(ylb);
title(tit);
cd dir
print flnm
%%%%%%%%

Cheers!

Rax

Subject: script inside function

From: Andy

Date: 14 Jan, 2011 18:48:04

Message: 4 of 5

"mashor housh" <mashor@technion.ac.il> wrote in message <ich1et$c8v$1@fred.mathworks.com>...
> I have script file which contains definition of parametes e.g. cd=2;
>
> when I use it inside a function e.g. :
>
> function []=abc()
> parameters
> cdes=cd;
> end
>
> the value of cdes is a chars of the current folder i.e. the current folder function 'cd' is recognized by matlab insted of the variable defined in the the script file 'paramters'.
> However, this problem does not occur if I use scrip inside script e.g. Mfiles contains:
>
> parameters
> cdes=cd
>
> why this happened? and where I can read about this in matlab help?
>
> Thank you,
> Mashor

I believe the short explanation of what is happening is that at the time of calling the function abc (before the script parameters has run), MATLAB decides that the 'cd' in 'cdes=cd' means the function 'cd'. It doesn't yet know that you intend to overload the function in a variable called 'cd' in the script parameters, because it doesn't parse the script parameters until it runs it.

The quick fix is to never do this ever. If you want to use the data from your script parameters, then make it a function so that you (and MATLAB) will always know what to expect to change in the current workspace whenever the function is called.

In response to Sean, this isn't an issue of overloading a builtin. This would have occurred with any function that was being overloaded. The problem is not the overloading, it's combining the script with the function.

Subject: script inside function

From: Rax

Date: 17 Jan, 2011 05:03:05

Message: 5 of 5

"Rax" wrote in message <igq48t$bqo$1@fred.mathworks.com>...
> "mashor housh" <mashor@technion.ac.il> wrote in message <ich1et$c8v$1@fred.mathworks.com>...
> > I have script file which contains definition of parametes e.g. cd=2;
> >
> > when I use it inside a function e.g. :
> >
> > function []=abc()
> > parameters
> > cdes=cd;
> > end
> >
> > the value of cdes is a chars of the current folder i.e. the current folder function 'cd' is recognized by matlab insted of the variable defined in the the script file 'paramters'.
> > However, this problem does not occur if I use scrip inside script e.g. Mfiles contains:
> >
> > parameters
> > cdes=cd
> >
> > why this happened? and where I can read about this in matlab help?
> >
> > Thank you,
> > Mashor
>
>
> Aha! I'm looking at a somewhat relater problem. I've written a script to plot triplet data and I would like to be able to specify a director to which an .eps of the generated graph will be saved, but cannot find a means of passing the directory name from the matlab command line through to the 'command line in the script' (is there an unambiguous terminology used to distinguish between these two?). Below is the relevant part of the script - any help would be appreciated.
>
> %%%%%%%%%
> function triplet_plot (data,fmt, xlb, ylb, tit, dir, flnm)
>
> x = data(:,1)';
> y = data(:,2)';
> e = data(:,3)';
>
> errorbar(x,y,e,fmt)
> xlabel(xlb);
> ylabel(ylb);
> title(tit);
> cd dir
> print flnm
> %%%%%%%%
>
> Cheers!
>
> Rax

I sorted this out - in case others were wondering the same:

there is a function version of cd and print that takes an argument i.e. cd() and print() if you then pass it a string (including the 'single quote'), it will allow you to specify a path and filename.

e.g. dir = 'mydirectory', flnm = 'myfile'

*including single quote* in the above sample code would print to myfile to mydirectory.

Cheers,

Rax

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