Info

This question is closed. Reopen it to edit or answer.

Transfer and execute function

3 views (last 30 days)
Christoph
Christoph on 1 Oct 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
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

Answers (1)

Iain
Iain on 1 Oct 2014
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 Comment
Christoph
Christoph on 1 Oct 2014
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!