Tilde character in the brackets - matlab

In MATLAB, what does the following code do:
[m, ~]=func_returning_matrix()
What does the tilde operator, ~, do?

In Matlab it means don't assign the corresponding output argument(s) from the function on the rhs of the assignment. So, if func_returning_matrix returns 2 arguments, the expression will assign the first to the variable m and forget the second. If func_returning_matrix returns 3 (or more) arguments, then the expression will drop the second and all later outputs from the function.

Related

Generalise calling of Matlab function with generic number of inputs

Suppose I have a pre-built Matlab function
function A=f(varagin)
...
end
I call f.m in a file main.m.
When calling f.m, the inputs to include depend on the number of columns of two matrices X and Y.
For example, if
d=2;
X=randn(4,d);
Y=randn(4,d);
then
A=f(X(:,1),X(:,2),Y(:,1),Y(:,2));
If
d=3;
X=randn(4,d);
Y=randn(4,d);
then
A=f(X(:,1),X(:,2),X(:,3),Y(:,1),Y(:,2),Y(:,3));
If
d=4;
X=randn(4,d);
Y=randn(4,d);
then
A=f(X(:,1),X(:,2),X(:,3),X(:,4),Y(:,1),Y(:,2),Y(:,3),Y(:,4));
Could you help me to generalise the calling of f.m in main.m with any d?
The answer is more or less identical to that of your previous question about indexing:
args = {arg1, arg2, arg3};
f(args{:});
args{:} generates a comma-separated list of its elements, which is thus equivalent to using each of its elements as an argument to the function.
To convert the columns of a numeric matrix to a cell array, use mat2cell.

Matlab symbolic function conversion without dot for matrix operation

When converting symbolic expression to matlabFunction, expression like
x=sym('x')
f=- x^3/6 + x
g=matlabFunction(f)
-> #(x)x-x.^3.*(1.0./6.0)
which is not what I want because x is gonna be a matrix and my application requires actual matrix multiplication such as x^3 instead of the dot product form of x.^3
The only way to get it working is to use anonymous function, i.e.
g=#(x) - x^3/6 + x
->#(x)-x^3/6+x
However, the issue with anonymous function is that I cannot use substitution but to type the entire formulation, i.e.
g=#(x) f
-> #(x)f which shows that expression substitution does not work
In short, I will need to solve either one of the technical difficulties: (1) If I use matlabFunction, how do I remove all the dot after the conversion? or (2) If I use anonymous function, how do I bypass typing the symbolic expression if I have already defined 'f' for the expression?
I am totally lost here and I hope someone familiar with matlab can give me 2 cents.
Thank you!
You can convert the sym object to a string when calculating the anonymous function:
g=#(x)eval(char(f))
Alternatively, you can use the following code
h=eval(['#(x)' char(f)])
instead of matlabFunction

What is the meaning of ~ apart from logical not?

What does following code do in Matlab? I searched documentation but ~ shows logical not. But I could not relate following output to anything about logical not.
[~, k ] = max([0.9 1.5 4.6; 3.31 0.76 5.4]
Output: 2 1 2
The ~ placeholder allows you to ignore an output from a function. Using this allows you to acknowledge that something is output by the function, but you do not have to allocate a variable to store the output in.
When a function returns values in Matlab the number of parameters it returns and the order of these parameters is important and allows you to know what each returned value is. You may sometimes come across situations where a function returns more values than you are interested in, and you can ignore the ones you are not interested in using ~.
In your example, M = max([0.9 1.5 4.6]) would return only the maximum value. If you want to know the index of the maximum value, you have to use [M,I] = max([[0.9 1.5 4.6]). If you need to know the index of the maximum value but are not interested in the actual value itself, you can use [~,I] = max([0.9 1.5 4.6]), and you thus do not need to allocate a variable to hold the maximum values.
The according reference you're looking for is the Symbol Reference, which states:
Tilde — ~
The tilde character is used in comparing arrays for unequal values,
finding the logical NOT of an array, and as a placeholder for an input
or output argument you want to omit from a function call. Not Equal to
...
Argument Placeholder
To have the fileparts function return its third output value and skip
the first two, replace arguments one and two with a tilde character:
[~, ~, filenameExt] = fileparts(fileSpec);
which is what #David suggested in his comment.

one example about arrayfun in matlab

I am new to matlab. I have the following code like this:
names=arrayfun(#num2str, 1:nseq_all, 'unif', 0);
After searching, I understand that arrayfun applies a function to each element of an array. So, I guess in this case, we apply num2str function to each element of the array 1:nseq_all. 'unif' and 0 are the arguments for the function and the corresponding value part. I have trouble to understand this part. Any comments are greatly appreciated.
'unif',0 is shorthand for 'UniformOutput',false, which means that the output does not have the same dimensions as the input array 1:nseq_all.
This is because a string 1 dimensions 1x1, but 124 has dimensions 1x3.
names will be a cell array, as a normal numeric array can't contain rows/columns with different numbers of elements.
Be sure to carefully read the arrayfun documentation.
'unif', 0 is shorthand for 'UniformOutput',false. Note that parameterName/parameterValue pairs in Matlab allow the abbreviation of the parameterName, as long as it doesn't conflict with another possible parameter. In this case 'un',0 would have worked as well. Anyway, what is that option for?
modifiedArray = arrayfun(function_handle, array)
applies the function defined in function_handle to each element of array and returns modifiedArray, which is the same size as array, and the class of whatever function_handle returns. This syntax can only be used if the output of function_handle is scalar, though class doesn't matter, so the output of function_handle can be a scalar structure, i.e. arrayfun(#(x)struct('field',x),magic(4)) is valid.
cellArray = arrayfun(function_handle, array, 'UniformOutput', false)
applies the function defined in function_handle to each element of array and returns cellArray, which is the same size as array. Each element of cellArray contains the output of a call of function_handle. This syntax must be used if the output of function_handle is non-scalar (even if each calculation returns a 1-by-2 array, which is totally uniform), but of course, you can use it with scalar output as well.
In your case, num2str returns a character array which is non-scalar if the argument of num2str goes above 9. Consequently, you need to set UniformOutput to false.

Simulink: How does one assign a double to a constant in a subsystem through the mask?

This is a slightly long-winded problem, but should be easy to follow along.
End goal: Input a 'double' vector into a C++ S Function by assigning it to a Constant.
Starting point: A function within the Dialog Callback of my masked subsystem returns the double vector that I need.
Steps in the middle: So I have a double vector as an input, I need a double vector as an output, but I cannot pass the vector directly, because set_param requires that I pass the value as a string.
Problem: What I tried is this: set_param(gcb,'refNameArray',num2str(refName));
where,
gcb (correctly) returns the block from within which I am doing
everything.
refNameArray is the parameter which needs to be assigned
the double vector
refName is the vector, in 'double' form, that I
have available within the dialog callback of the mask.
I use num2str because I cannot directly assign a vector, I must input it as a string.
But, even using num2str, I get an error saying Invalid Setting in Block <blockName> for parameter 'Value'.
Any suggestions?
The value you set for param in the dialog should be syntactically similar to an RHS expression in MATLAB. For example, for a vector value you would need to set it as '[1 3 4]'. Notice the square brackets. num2str does not add that square brackets. You can either add it manually at the ends before calling set_param or you can use mat2str which will create the string with the brackets.