Function inside script in Matlab? [duplicate] - matlab

This question already has answers here:
In MATLAB, can I have a script and a function definition in the same file?
(7 answers)
Closed 9 years ago.
Can I have fast and short helper local function to use inside script?
Currently I have "FUNCTION keyword use is invalid here" message.
Why?

This is correct, MATLAB does not allow you to define full functions in a script. However, there are at least two solutions that may help you:
You could turn the script into a function. Workspace variables you are referring to from within your scripts would become arguments of the function, and you could return certain result variables.
If your helper function is really small, you may be able to define it in your script as an anonymous functions without the function keyword, as in poly = #(x) x.^2 + 3 * x - 4; - a polynomial function, for example.

Related

matlab function variable definition [duplicate]

This question already has answers here:
MATLAB not enough input arguments
(2 answers)
Closed 5 years ago.
In Matlab, I want to get the variables from the workspace for the function . But I did not do it.
For example; the function is:
function Y = objfun(x)
Y = 20+x(1).^2 + 2*x(2).^2 -15*x(3);
end
gives me the following problem when I run the function
>> objfun
Not enough input arguments.
Error in objfun (line 5)
Y = 20+x(1).^2 + 2*x(2).^2 -15*x(3);
x variable is exist in workspace like x= [4 5 7] and I don't want to write it inside of function. so what shall I do.
Maybe it is very east question for you but I don't know and I trid make it.
could you help me?
In Matlab (or Octave) you can use scripts or functions.
If you create script called objfun, you have what you are looking for. Just call it using objfun and it will use workspace variable x. The script is saved as objfun.m.
Functions are different. They can have arguments, but these arguments are local variables (only available within the function).
If you define a function, you must call it with the arguments.

Passing two values from one Matlab function to another in one line [duplicate]

This question already has answers here:
How to force MATLAB to return all values in a nested function call?
(4 answers)
Closed 6 years ago.
I'm looking to pass the outputs of a two-output function into a two-input function, in one line.
i.e. if I have two functions
function [out1, out2] = funA(in)
%function definition here
function out = funB(in1, in2)
%function definition here
I want to do something like
out = funB(funA(in)) %this doesn't actually work
Is there syntax to do this without having to write it as
[o1, o2] = funA(in)
out = funB(o1, o2)
I'm also not looking for
[o1, o2] = funA(in); out = funB(o1, o2);
I'm not sure this is possible as if you call the function in-line with another call, Matlab will always assume that you only want the first/primary output.
Matlab only creates the other output variables (out2/in2 here) if you actually assign them.

How to change parameters in a function when using ode45 in MATLAB [duplicate]

This question already has an answer here:
MATLAB: How do I pass a parameter to a function?
(1 answer)
Closed 6 years ago.
I have a function (for an SIR model), and then a script that solves this function and compares it to the data I am trying to fit this model to. Thus I am trying to run a for loop to change a parameter in the function in order to optimize the fit. I'm wondering how to change my the (r) and (a) parameters in a for loop without having to change them by hand:
function ydot=epidemic(t,y)
r=0.000001;
a=1/3;
ydot=zeros(3,1);
ydot(1)=-r*y(1)*y(2);
ydot(2)=r*y(1)*y(2)-a*y(2);
ydot(3)=a*y(2);
end
and
[t,y]=ode45('epidemic',[0:222], [70500,1,0])
Thanks
You can use the following: you add the r and a parameter to your function
function ydot=epidemic(t,y,r,a)
ydot=zeros(3,1);
ydot(1)=-r*y(1)*y(2);
ydot(2)=r*y(1)*y(2)-a*y(2);
ydot(3)=a*y(2);
end
and then pass the function to ode45 like that
r = 0.000001 ;
a = 1/3 ;
[t,y]=ode45(#(t,y)epidemic(t,y,r,a),[0:222], [70500,1,0])
Basically, #(t,y)epidemic(t,y,r,a) defined a new function with arguments (t,y) and where r and a are using the values defined just above.
Then you can put all of that in a for loop.

significance of ~ symbol in matlab function [duplicate]

This question already has answers here:
suppressing output variables in matlab
(2 answers)
Closed 8 years ago.
If I have a function a that accepts 2 parameters (double) in Matlab as follows
function [x,y] = a(z)
What does the symbol "~" do when the function is called with this handle as follows
[x,~,y] = a[10]
Thanks
The "~" symbol in matlab is logical NOT. So it's basically like ignoring that output/input. For example, if I have a line of code like this:
[out1,~,out3] = function(vargin);
the second output is not kept or stored anywhere for later use. For more info, type "help ~" in the command window.

Matlab get string containing variable name [duplicate]

This question already has answers here:
print variable-name in Matlab
(4 answers)
Closed 9 years ago.
In Matlab, how can i get a String containing "GRUMPY" given the following declaration:
GRUMPY = 500;
This is usually called reflection in other programming languages, but i cannot find an example of it in Matlab.
MATLAB doesn't provide built-in functionality for this, but there is a workaround, as employed here
Essentially, you have to create your own function to do this. Take advantage of Matlab's functionality for getting the variable name of the INPUT ARGUMENT to a function.
I.e.
function out = varname(var)
out = inputname(1);
end
Then
GRUMPY = 500;
name = varname(GRUMPY)
will give you what you want.
If I understand correctly you should try
who GRUMPY
or
which GRUMPY