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 件のコメント

John D'Errico
John D'Errico 2016 年 7 月 8 日
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.
Image Analyst
Image Analyst 2016 年 7 月 8 日
Then how about this:
function mFunction(alpha,beta)
return;
That's pretty minimal, and doesn't load anything.
amine&&
amine&& 2016 年 7 月 8 日
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 2016 年 7 月 9 日
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&& 2016 年 7 月 9 日
Ok Image Analyst, it is very interesting what you say I'll take it in concideration. Thanks!

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2016 年 7 月 8 日

0 投票

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 件のコメント

amine&&
amine&& 2016 年 7 月 8 日
is what I have to create two functions (run_my_plot and mFunction)?
Walter Roberson
Walter Roberson 2016 年 7 月 8 日
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&& 2016 年 7 月 9 日
What value i have to give for alpha and beta? Thanks!
amine&&
amine&& 2016 年 7 月 9 日
is what I need to take :
[alpha,beta] = meshgrid(0:.2:1, 0:.2:1);
thanks!
Image Analyst
Image Analyst 2016 年 7 月 9 日
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&& 2016 年 7 月 9 日
編集済み: amine&& 2016 年 7 月 9 日
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 2016 年 7 月 9 日
編集済み: Walter Roberson 2016 年 7 月 10 日
[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&& 2016 年 7 月 9 日
Thanks Roberson, it works perfectly!
amine&&
amine&& 2016 年 7 月 10 日
How can i get "current function value" giving by Optimization tool using only the command line. Thanks
Walter Roberson
Walter Roberson 2016 年 7 月 11 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by