Changing the declaration of a function?

I have a following function that begins with:
function X=mFunction(alpha,beta)
%%load the dataset
filename = 'Bo_1.xlsx';
y = xlsread(filename);
yS=y(1:288);
I want to minimize it. But without load data every time..
How I should proceed?
Thanks!

5 comentarios

John D'Errico
John D'Errico el 8 de Jul. de 2016
Oh, come on. Learn to use MATLAB. How many questions can you ask about the same function? In this case, learn to pass in parameters into a function. That merely means you need to learn to use function handles, something you should have learned several questions ago.
Then how about this:
function mFunction(alpha,beta)
return;
That's pretty minimal, and doesn't load anything.
amine&&
amine&& el 8 de Jul. de 2016
Ok John, I'll see the MATLAB documentation about function handles. Image Analyst i don't understend your answer. Thanks for your help!
Image Analyst
Image Analyst el 9 de Jul. de 2016
You said you wanted to minimize it, so I just threw out everything that wasn't absolutely needed for the function to run. You assign a filename and read in a workbook but don't actually return it to your calling function, so I got rid of that unneeded part. From your comment later to Walter it sounds like you don't even know what alpha and beta are, so you might not need those either, and the function could be simplified even more - just don't have any input arguments. Now you'll have a function that does absolutely nothing - about as minimal as you can get.
amine&&
amine&& el 9 de Jul. de 2016
Ok Image Analyst, it is very interesting what you say I'll take it in concideration. Thanks!

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 8 de Jul. de 2016
function run_my_plot
alpha = ....
beta = ....
filename = 'Bo_1.xlsx';
y = xlsread(filename);
yS=y(1:288);
X = arrayfun(alpha, beta, yS);
surf(alpha, beta, X, 'edgecolor', 'none');
function X = mFunction(alpha, beta, yS)
.... code with no load() goes here

10 comentarios

amine&&
amine&& el 8 de Jul. de 2016
is what I have to create two functions (run_my_plot and mFunction)?
Walter Roberson
Walter Roberson el 8 de Jul. de 2016
No, the code from alpha to surf can be in a script file rather than inside a function like I show. However, if that code is in a script instead of in a function then your mFunction needs to be in a separate file, because it is not permitted to have a script and a function in the same .m file. The arrangement I showed, with the two functions, can both go in the same run_my_plot.m file.
amine&&
amine&& el 9 de Jul. de 2016
What value i have to give for alpha and beta? Thanks!
is what I need to take :
[alpha,beta] = meshgrid(0:.2:1, 0:.2:1);
thanks!
There's no possible way for us to know if this (alpha and beta) is good for you or not. All you said was that you're starting with this:
function X=mFunction(alpha,beta)
%%load the dataset
filename = 'Bo_1.xlsx';
y = xlsread(filename);
yS=y(1:288);
and that you don't want to load data every time. We're not even sure if there is anything later down in the function that you did not include. For example, you compute yS but never use it in the code you've shown. Why not? You also pass in alpha and beta but never show them being used, so how are we supposed to know what they might be or be used for? You said you don't want to load the data in every time, so maybe that means you just read in y outside that function (in your calling routine) and then pass y into mFunction() from the calling routine via mFunction's input argument list
function X=mFunction(y, alpha, beta)
yS=y(1:288);
But again, yS is not returned or used ever, so who knows what this function is supposed to do? Moreover, you (try to) return X but you never set X in the function, so that will throw an error.
Please read this link so we can help you better.
amine&&
amine&& el 9 de Jul. de 2016
Editada: amine&& el 9 de Jul. de 2016
when I want to draw this function I get the following error:
[X,Y] = meshgrid(0:.2:1, 0:.2:1);
Z = arrayfun(mFunction, X, Y);
Error using mFunction (line 16)
Not enough input arguments.
Thanks!
Walter Roberson
Walter Roberson el 9 de Jul. de 2016
Editada: Walter Roberson el 10 de Jul. de 2016
[X,Y] = meshgrid(0:.2:1, 0:.2:1);
filename = 'Bo_1.xlsx';
y = xlsread(filename);
yS=y(1:288);
Z = arrayfun(@(alpha,beta) mFunction(alpha,beta,yS), X, Y);
surf(X, Y, Z, 'edgecolor', 'none')
amine&&
amine&& el 9 de Jul. de 2016
Thanks Roberson, it works perfectly!
amine&&
amine&& el 10 de Jul. de 2016
How can i get "current function value" giving by Optimization tool using only the command line. Thanks
Walter Roberson
Walter Roberson el 11 de Jul. de 2016
That is a question that has nothing to do with this topic. Fortunately you have already opened a new Question about it, which has been replied to.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 8 de Jul. de 2016

Comentada:

el 11 de Jul. de 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by