significance of ~ symbol in matlab function [duplicate] - matlab

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.

Related

# in Function definition in Matlab [duplicate]

This question already has answers here:
What is the # operator (at sign) in MATLAB?
(3 answers)
#(t) mean in Matlab? [duplicate]
(2 answers)
Closed 4 years ago.
I was just browsing through a code and I found the following line :
other_function(#(t)(xx(t,g)))
where other_function,xx are already defined functions and g is already defined.
Here is the code for xx
function [val]=xx(x,y)
val=x+y;
end;
SO now I am unable to understand the meaning of #(t)(xx(t,g))
It is a function handle. It is useful to pass functions as parameters. You can find more in the MATLAB documentation
Just an example: suppose you have a simple function
function y = computeSquare(x)
y = x.^2;
end
than you can compute an integral in this way:
q = integral(#computeSquare,0,1);
In your example: other_function declares as an input parameter a function t and another parameter called g.

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.

What does ~ mean inside the brackets of a function call in MATLAB? [duplicate]

This question already has answers here:
suppressing output variables in matlab
(2 answers)
Closed 8 years ago.
For example, I have a function
[power, capacity] = function_name(users, distance, radius)
and the function call is
[~, capacity] = function_name(users, distance, 5);
~ just means that you don't want to store the result in any variable.
here is a detailed explanation:
http://www.mathworks.com/matlabcentral/answers/72537-what-does-a-tilde-inside-square-brackets-mean
And the most relevant section: "when you use [~,palette], that means that you just want the second output of your function, and do not care the first one."

Function inside script in Matlab? [duplicate]

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.

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