PDA

查看完整版本 : How to Suppress Function Output


poster
2019-11-24, 15:00
<div class="content"><!--introduction--><p>You may have noticed that we have some functions that return no output if none is requested, and yet some functions always return something, even if not asked. What's a good way to achieve this in MATLAB?</p><p><i>I do want to point out that while we used to use this programming pattern more often, we have tended to back away from it more recently. For example, you may notice we have two functions for histograms, <tt><a href="https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.histogram.html">histogram</a></tt> and <tt><a href="https://www.mathworks.com/help/matlab/ref/histcounts.html">histcounts</a></tt>; one for the graphics and one for the computation.</i></p><!--/introduction--><h3>Contents</h3><div><ul><li><a href="#eacb0346-4aef-4dc6-991b-306b18189758">Example</a></li><li><a href="#354a394f-85a3-4211-a1a1-29dc582dd974">Coding Possibility One</a></li><li><a href="#8b40fe5f-568c-4223-bfaa-eaabde5c3948">Coding Possibility Two</a></li></ul></div><h4>Example<a name="eacb0346-4aef-4dc6-991b-306b18189758"></a></h4><p>The function <tt><a href="https://www.mathworks.com/help/matlab/ref/stairs.html">stairs</a></tt> is a prime example.</p><p>Compare</p><pre class="codeinput">array = randperm(50);
stairs(array)
</pre><img vspace="5" hspace="5" src="https://blogs.mathworks.com/images/loren/2019/suppressOutput_01.png" alt=""> <p>with this</p><pre class="codeinput">s = stairs(array)
<span class="comment">%</span>
<span class="comment">% You can see that in each case, we get a plot. Only when we ask explicitly</span>
<span class="comment">% for an output do we receive one.</span>
</pre><pre class="codeoutput">s =
Stair with properties:

Color: [0 0.4470 0.7410]
LineStyle: '-'
LineWidth: 0.5000
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [1&times;50 double]
YData: [1&times;50 double]

Use GET to show all properties
</pre><img vspace="5" hspace="5" src="https://blogs.mathworks.com/images/loren/2019/suppressOutput_02.png" alt=""> <h4>Coding Possibility One<a name="354a394f-85a3-4211-a1a1-29dc582dd974"></a></h4><p>In this example, you will see code written the way we used to (and perhaps still do) in MATLAB when we want to return no outputs when called without any output arguments, i.e., when <tt><a href="https://www.mathworks.com/help/matlab/ref/nargout.html">nargout</a> == 0</tt></p><pre> function y = attempt1(x)
yy = sin(x);
if nargout > 0 % OR if nargout
y = yy;
end
end</pre><p>In this case, it's pretty simple. If we want an output, at the end we simply copy the output to the variable with the right output name. If we have many possible output variables, we still need to treat only the first output variable specially.</p><pre> function [y,z1,z2,z3] = attempt1a(x)
yy = sin(x);
z1 = yy*17;
z2 = yy+17;
z3 = yy^17;
if nargout > 0 % OR if nargout
y = yy;
end
end</pre><h4>Coding Possibility Two<a name="8b40fe5f-568c-4223-bfaa-eaabde5c3948"></a></h4><p>In the second scheme, we write the code with the variable names we want, and then <tt><a href="https://www.mathworks.com/help/matlab/ref/clearvars.html">clearvars</a></tt> either just the entire local workspace, or, if we prefer, just the first one, like above.</p><pre> function y = attempt2(x)
y = sin(x);
if ~nargout % if nargout == 0
clearvars % or simply clearvars y
end
end</pre><p>While I hate using functions or commands like <tt>clear</tt> in function code, I think in this use case, it's okay. When it is NOT okay is when you are trying to coerce the operating system to give up memory. Since MATLAB calls the OS to manage memory, MATLAB is not in control of the timing when the memory it releases actually is available again outside MATLAB. Another technique for clearing memory that's no longer needed is to set the variable(s) whose memory you want reduced to the empty array, e.g., <tt>myvar = []</tt>.</p><p>Do You Have a Preference? Which code pattern to you prefer? Why? Let us know <a href="https://blogs.mathworks.com/loren/?p=3333?respond">here</a>.</p><script language="JavaScript"> <!--
function grabCode_d340ebb941894495a768f85d29ad6830() {
// Remember the title so we can use it in the new page
title = document.title;

// Break up these strings so that their presence
// in the Javascript doesn't mess up the search for
// the MATLAB code.
t1='d340ebb941894495a768f85d29ad6830 ' + '##### ' + 'SOURCE BEGIN' + ' #####';
t2='##### ' + 'SOURCE END' + ' #####' + ' d340ebb941894495a768f85d29ad6830';

b=document.getElementsByTagName('body')[0];
i1=b.innerHTML.indexOf(t1)+t1.length;
i2=b.innerHTML.indexOf(t2);

code_string = b.innerHTML.substring(i1, i2);
code_string = code_string.replace(/REPLACE_WITH_DASH_DASH/g,'--');

// Use /x3C/g instead of the less-than character to avoid errors
// in the XML parser.
// Use '\x26#60;' instead of '<' so that the XML parser
// doesn't go ahead and substitute the less-than character.
code_string = code_string.replace(/\x3C/g, '\x26#60;');

copyright = 'Copyright 2019 The MathWorks, Inc.';

w = window.open();
d = w.document;
d.write('<pre>\n');
d.write(code_string);

// Add copyright line at the bottom if specified.
if (copyright.length > 0) {
d.writeln('');
d.writeln('%%');
if (copyright.length > 0) {
d.writeln('% _' + copyright + '_');
}
}

d.write('</pre>\n');

d.title = title + ' (MATLAB code)';
d.close();
}
--> </script><p style="text-align: right; font-size: xx-small; font-weight:lighter; font-style: italic; color: gray"><br><a href="javascript:grabCode_d340ebb941894495a768f85d29ad6830()"><span style="font-size: x-small; font-style: italic;">Get
the MATLAB code <noscript>(requires JavaScript)</noscript></span></a><br><br>
Published with MATLAB&reg; R2019a<br></p></div><!--
d340ebb941894495a768f85d29ad6830 ##### SOURCE BEGIN #####
%% How to Suppress Function Output
% You may have noticed that we have some functions that return no output if
% none is requested, and yet some functions always return something, even
% if not asked. What's a good way to achieve this in MATLAB?
%
% _I do want to point out that while we used to use this programming pattern
% more often, we have tended to back away from it more recently. For
% example, you may notice we have two functions for histograms,
% |<https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.histogram.html
% histogram>| and
% |<https://www.mathworks.com/help/matlab/ref/histcounts.html histcounts>|;
% one for the graphics and one for the computation._
%% Example
% The function |<https://www.mathworks.com/help/matlab/ref/stairs.html
% stairs>| is a prime example.
%
% Compare
array = randperm(50);
stairs(array)
%%
% with this
s = stairs(array)
%
% You can see that in each case, we get a plot. Only when we ask explicitly
% for an output do we receive one.
%% Coding Possibility One
% In this example, you will see code written the way we used to (and
% perhaps still do) in MATLAB when we want to return no outputs when called
% without any output arguments, i.e., when
% |<https://www.mathworks.com/help/matlab/ref/nargout.html nargout> == 0|
%
% function y = attempt1(x)
% yy = sin(x);
% if nargout > 0 % OR if nargout
% y = yy;
% end
% end
%
% In this case, it's pretty simple. If we want an output, at the end we
% simply copy the output to the variable with the right output name. If we
% have many possible output variables, we still need to treat only the
% first output variable specially.
%
% function [y,z1,z2,z3] = attempt1a(x)
% yy = sin(x);
% z1 = yy*17;
% z2 = yy+17;
% z3 = yy^17;
% if nargout > 0 % OR if nargout
% y = yy;
% end
% end

%% Coding Possibility Two
% In the second scheme, we write the code with the variable names we want,
% and then |<https://www.mathworks.com/help/matlab/ref/clearvars.html
% clearvars>| either just the entire local workspace, or, if we prefer,
% just the first one, like above.
%
%
% function y = attempt2(x)
% y = sin(x);
% if ~nargout % if nargout == 0
% clearvars % or simply clearvars y
% end
% end
%
%
% While I hate using functions or commands like |clear| in function code, I
% think in this use case, it's okay. When it is NOT okay is when you are
% trying to coerce the operating system to give up memory. Since MATLAB
% calls the OS to manage memory, MATLAB is not in control of the timing
% when the memory it releases actually is available again outside MATLAB.
% Another technique for clearing memory that's no longer needed is to set
% the variable(s) whose memory you want reduced to the empty array, e.g.,
% |myvar = []|.
%
% Do You Have a Preference?
% Which code pattern to you prefer? Why? Let us know
% <https://blogs.mathworks.com/loren/?p=3333?respond here>.
##### SOURCE END ##### d340ebb941894495a768f85d29ad6830
--><img src="http://feeds.feedburner.com/~r/mathworks/loren/~4/6r0ijWmgpfg" height="1" width="1" alt=""/>

more... (http://feedproxy.google.com/~r/mathworks/loren/~3/6r0ijWmgpfg/)