Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Transfer and execute function

조회 수: 1 (최근 30일)
Christoph
Christoph 2014년 10월 1일
마감: MATLAB Answer Bot 2021년 8월 20일
Hello Matlab users,
I want to store the values for a function in the first lines. Is possible to save the function in the main script?
Otherwise is it possible to transfer the function info from the main-script into the fuction-file via variables?
for example: function file starts left_side_of_equation right_side_of_equation unknown variables... function file ends
main-script starts function diff = fit_biexp(x,TR,Y,time_stamps) %start unknown variable A=x(1); B=x(2); T1=x(3); %end unkown variable
diff = sum(((rechte_part_of_equation))-left_part_of_equation).^2); end main-script starts
If it is possible, how? I already tried various ways without success.
Thanks, Chris

답변 (1개)

Iain
Iain 2014년 10월 1일
I don't quite get what you're asking:
Are you saying that you want to use a variable function for something? In which case you should be able to use anonymous functions. For example:
my_function = @(x,y)(sin(x)*3 + cos(x)*4 + sin(y)*3 + cos(y)*4);
a = [0 1 2], b = [1; 2; 3; 4], c = bsxfun(my_function,a,b)
Or are you asking for multiple outputs from a function? You can do that by defining a function like this:
function [output1 output2 varargout] = my_function(a,b,c)
output1= a*b*c;
output2 = a^b^c;
if nargout > 2
varargout{1} = c - b - a;
if nargout > 3
varargout{2} = c - b - a;
end
end
You can then call your function as normal: [out1 out2 optional1 optional2] = my_function(1,2,3)
  댓글 수: 1
Christoph
Christoph 2014년 10월 1일
Hello Lain,
thanks a lot for your answer. I want to make a matlab script user-friendly. The issue is that I have a function in which the equation changes from time to time. This equation might also have 1 ,2 ,3 or 4 unknown parameters. All these dynamical values should be embedded in the main executing file. Therefore I have two options:
Option A: Implement the function in the main file (where I press the play button).
Option B: Transfer the values from the main file to the function file via variables.
I hope that´s better explained?
BR, Chris

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by