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 Comments

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.
Ok John, I'll see the MATLAB documentation about function handles. Image Analyst i don't understend your answer. Thanks for your help!
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.
Ok Image Analyst, it is very interesting what you say I'll take it in concideration. Thanks!

Sign in to comment.

 Accepted Answer

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 Comments

is what I have to create two functions (run_my_plot and mFunction)?
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.
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.
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!
[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')
Thanks Roberson, it works perfectly!
How can i get "current function value" giving by Optimization tool using only the command line. Thanks
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!