Matlab get string containing variable name [duplicate] - matlab

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

Related

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.

Is it possible to transform a string to a variable code name in MATLAB? [duplicate]

This question already has answers here:
Create variables with names from strings
(6 answers)
Closed 6 years ago.
This might be a very bizarre question (and probably I might be advised against doing such a weird thing) but if I have a string as in v_str ='var_name' and I want to transform the contents of that code into the actual code, is that possible in MATLAB? As in:
v_str = 'var_name'
x = make_string_to_code(v_str)
translates to the functioning code:
x = var_name
which simply transforms the string to actual code.
The only way I thought of doing this is by writing a file with that code and then on the next line executing that fine, but I wanted to avoid writing files every time that I want to do this.
Also, why is this so not recommended? Why is it so bad?
You can use eval, but that doesn't mean you should, as others mentioned.
v_str = '2 + 2'
eval(['x = ' v_str]) % x = 4;
x = eval(v_str); % x = 4;
The documentation of eval can be found here: eval

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.

What are some good ways to emulate cascading indexing in MATLAB? [duplicate]

This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 8 years ago.
E.g. I would like to do things such as:
A=4:20;
find(A>5)(2) % want to access the 2nd element of the index array returned by find
Yes, this comes up fairly frequently in different contexts, and the one-line answer is subsref. For your case, it is this:
subsref(find(A>5),struct('type','()','subs',{{2}}))
A much cleaner solution uses an undocumented builtin:
builtin('_paren',find(A>5),2)
As an alternative to ugly syntax or undocumented functionality, you could define a small function like the following,
function outarray = nextind(inarray,inds)
outarray = inarray(inds);
Or an inline function:
nextind = #(v,ii) v(ii);
And call it like nextind(find(A>5),2). This is cleaner than subsref and good if you are doing linear indexing (not subscripts).

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.