"Can a function known whether/which specific output is ignored ?"
No. nargout tells you the number of output arguments specified in the caller but it counts outputs suppressed with a tilde (~). nargout(fun) tells you the number of ouputs a specific function has to offer but that tells you nothing about the caller.
What are the alternatives?
1. Pass an input to the function that specifies which outputs you're requesting. The tricky part is to remember to change that input value if a different set of outputs is requested.
2. Prioritize your outputs and use nargout to determine which outputs to produce.
You can also try the new and improved (vs.2.0.0) detectOutputSuppression() from the file exchange. It reads the caller line and parses the outputs to determine if any are suppressed. Example
The main() function calls myFunc() and suppresses the 2nd and 3rd outputs.
function main()
[mst(1), ~, ~, data] = myFunc();
function [a, b, c, d] = myFunc()
a = 1; b = 2; c = 3; d = 4;
isTilde = detectOutputSuppression(nargout);
Result: isTilde = [0 1 1 0].
Supported syntaxes
- [a,~,~,d] = myFunc();
- a = myFunc();
- [a(1),a(2),~] = myFunc();
- [~,~,c(1:20),~] = myFunc();
- [~,b{3}] = myFunc();
- [~,b{3,1,5}] = myFunc();
- [~,~,~,g{1}{2}{4}(1)] = myFunc();
- q=cell(1); [~,q{:}] = myFunc(); % because q is 1x1.
- T=table(); [T.a,~] = myFunc();
- [j,u,n,k]; [~,~,~,~] = myFunc(); [j,u,n,k]
- [j,u,n,k], [~,~,~,~] = myFunc(), [j,u,n,k]
- S=struct; [S.x,~,S.t(4)] = myFunc();
- [v.a, v.b{1}, v.c(2,2), ~] = myFunc();
- myFunc + 1;
- y = [1, myFunc(), 1];
- assert(~isempty(myFunc()))
- [a,b,~,~] = myFunc(...
- inputs); %split in multiple lines after function name
- and more....
Syntaxes not supported
An error message is thrown when these syntaxes are detected.
- Outputs not separated by commas: [a b c] = myFunc()
- When myFunc() is not called from within an m-file or wasn't the most recent command called from the command window.
- When myFunc() is called in debug mode but isn't the line currently paused or executed by MATLAB.
- Output assignment to comma separated lists: x=cell(1,5); [x{:}] = myFunc();
- Caller line is split into multiple lines before the inputs (a split after the function name is OK).
- When myFunc() is wrapped in an anonymous function: f = @myFunc; f()
- When the parser matches more than one [...]=myFunc per line of code.
Best Answer