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.
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)?
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!
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&& 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
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개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2016년 7월 8일

댓글:

2016년 7월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by