How to determine if a function has been called, followed by a semicolon (";")?

In a Matlab script, I call a user-defined function (m-function). My function returns a value when printing a value in a command window using calls dispand / or fprintf.

When writing an expression or instruction, one of them puts ;on the suppression of printing. When an expression calls my function, it ;can suppress the print of the return value. However, this does not affect the output dispinside the called function.

I want to exclude function display output when necessary. Is there a way to determine if a function call was made in an expression ending in; ?

+4
source share
5 answers

I like the spirit of what you are trying to do, but I think this probably goes against the general programming patterns in Matlab. As you correctly state, the goal of the end semicolon is to suppress the printing of return values. Trying to make it turn on your other function may well require some deep hacking and ugly hard code. The standard way to implement what you are describing is with the arguments of the name-value pair of the property. For example, the Matlab optimization set has a property 'Display'that can be set to various values ​​to indicate the desired level of detail (see optimset).

, mint, mlintmex mtree - . , mlint, , "Terminate statement with semicolon to suppress output" (. MatlabCental) t , .

, , , . , , , . , , , ....

st = dbstack('-completenames');  % M-file of caller and line number
caller = st(2);
str = mlint('-lex',caller.file); % Struct containing parsed data

isSemicolon = false; % Assume no semicolon
for i = 1:length(str)
    % Find end-of-line corresponding to function call
    c = strsplit(str(i).message,'/');
    if str2double(c{1}) == caller.line && strcmp(c{2}(end-5:end-1),'<EOL>')
        % Check for comments
        j = i-1;
        if ~isempty(strfind(str(j).message,'%'))
            j = j-1;
        end
        % Check for semicolon
        if strcmp(str(j).message(end-2),';')
            isSemicolon = true; % Semicolon found
            break;
        end
    end
end

, , , .

+3

, . .

" ", , MATLAB display . , MATLAB :

y = myFunc(x)

:

y = myFunc(x);
display(y);

, "print" "verbose" - , .

+1

, (.. disp) - . , (, ):

function y=myFunc(a,displayResults)
if nargin==1
    displayResults=true; %// set the default behaviour
end

%// if you want to print something
if displayResults
    disp(a)
end
end

. foo , , :

function [x,m] = foo(a)
m={}; %// initialise cell array of output messages

x=a;

m{length(m)+1}=a; %// some message
m{length(m)+1}='another message'; %// another message
end

, , fprintf, m , . , displayResults , .

+1

( - per-se, ). , :

function y = prt_test(x)
    y = x + 1;
    disp('IN PRT_TEST')
end

:

>> % Regular use - message and output are displayed:
>> y = prt_test(1)
IN PRT_TEST
y =
     2

>> % Regular use w/ ";" - only message is displayed:
>> y = prt_test(2);
IN PRT_TEST

>> % Use "evalc()" - message and output are displayed:
>> evalc('y = prt_test(3)')
ans =
IN PRT_TEST
y =
     4

>> % Use "evalc()" w/ func ";" - only message is displayed:
>> evalc('y = prt_test(4);')
ans =
IN PRT_TEST

>> % Use "evalc();" - no output:
>> evalc('y = prt_test(5)');

>> % Use "evalc();" w/ func ";" - no output:
>> evalc('y = prt_test(6);');

>>
0

disp :

function [] = hideDisplay()
%[
    % Override `disp` behavior
    disp = @(x)doNothing;

    % Next `disp` calls will no longer appear in matlab console
    disp('Hello')
    disp('World!')
%]
end

%% ---
function [] = doNothing()
%[
%]
end
0

All Articles