I have some Matlab function call as
[Hz,~] = freqz(num, den, f, fsA)
What "~" means or what does it represent?

 Risposta accettata

dpb
dpb il 18 Ott 2022
Modificato: dpb il 18 Ott 2022
It is a placeholder for the optional second argument but indicates to not assign to a variable -- in the above call, there is no reason to use it; it is the same as if had written
Hz = freqz(num, den, f, fsA);
the second output (and any/all subsequent) will just go to the bit bucket.
There would be reason to use it, and its raison d'etre would be if the expression were instead
[~,w] = freqz(num, den, f, fsA);
there it is the placeholder for the frequency response first output variable but for some reason that wasn't needed; only the second output, the angular frequency values were required. It saves creating unwanted/unneeded variable(s) in the workspace.
BTW -- This is a badly-named return variable in the original code; the first return argument from freqz is the response magnitude; 'Hz' implies a frequency; the third output variable is, in fact, the frequency vector in hertz.
PS. I know the syntax is in the doc somewhere, but one can't search for the single character punctuation and it isn't mentioned at all in the basics of function creation...so I never came across where that is.

6 Commenti

Steven Lord
Steven Lord il 18 Ott 2022
Related to the PS: searching for ~ in the online documentation lists the documentation page for the not, ~ function/operator as the first hit. The Tips section of that page states that you can also use ~ as a placeholder and links to a page that discusses ignoring inputs in function definitions.
It should probably also link to the Ignore Function Outputs page that's a Related Topic for the Ignore Inputs in Function Definitions page; I'll note that to the documentation staff.
Walter Roberson
Walter Roberson il 18 Ott 2022
Modificato: Walter Roberson il 18 Ott 2022
https://www.mathworks.com/help/matlab/matlab_prog/ignore-function-outputs.html
also mentioned in the tips for https://www.mathworks.com/help/matlab/ref/not.html and in the description of tilde in https://www.mathworks.com/help/matlab/matlab_prog/matlab-operators-and-special-characters.html#bvg3oy_-5
dpb
dpb il 18 Ott 2022
"searching for ~ in the online documentation..."
But the @doc link in Answers doesn't bring up anything is where one is in the forum...it's a lot more effort to have to go delve into the main doc and search there just to add a little to an answer when/if pressed for time.
Note that there are some functions where it makes a difference whether you output to a single output or if you specify an output list with ~ as the trailing outputs. For example
A = rand(5,7,4);
b = size(A); %b is 5 7 4
[b, ~] = size(A); %b is 5
[b, c] = size(A); %b is 5 c is 28
[b, c, ~] = size(A); %5 and 7
[b, c, d] = size(A); %5, 7, and 4
Also: it is not possible for a function to detect that ~ has been used. Functions can only detect the number of output positions.
Walter Roberson
Walter Roberson il 19 Ott 2022
the @doc facility misses a number of things. For example you cannot even reference toolboxes by name.
dpb
dpb il 19 Ott 2022
It also often lists many toolbox overloaded functions before the base product.
It's annoying it only works if click on with the mouse, typing more than the @ sign goes to name match and then tab won't/doesn't select the matched but have to go click on it, too...but it's still better than having to go open the full doc directly, just could be better...

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2022b

Richiesto:

il 18 Ott 2022

Commentato:

dpb
il 19 Ott 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by