如何确定是否在调用函数后接分号(“;")? [英] How to determine if a function was called followed by a semicolon (";")?

查看:101
本文介绍了如何确定是否在调用函数后接分号(“;")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Matlab脚本中,我称为用户定义函数(m函数).在使用disp和/或fprintf调用将值打印到命令窗口时,我的函数返回一个值.

In a Matlab script I call a user-defined function (m-function). My function returns a value while printing the value to the command window using disp and/or fprintf calls.

在编写表达式或语句时,将;放在其末尾以禁止打印.当表达式调用我的函数时,;可以禁止显示返回值.但是,这不会影响所调用函数中的disp输出.

When writing an expression or a statement, one puts ; at its end to suppress printing. When the expression calls my function, the ; can suppress the printing of the returned value. However, this does not effect the disp output from within the function called.

我想在适当的时候取消该功能的显示输出. 是否可以确定是否在以; 结尾的表达式中进行了函数调用?

I want to eliminate the display output of the function when appropriate. Is there a way to determine whether a function call was made in an expression ending with ;?

推荐答案

我喜欢您尝试做的工作的精神,但是我认为这可能与Matlab中的常见编程模式背道而驰.正如您正确指出的那样,终止分号的目的是禁止打印返回值.试图将其合并到其他功能中可能会需要一些深层次的技巧和难以维护的丑陋代码.实现您所描述内容的标准方法是通过属性名称-值对参数.例如,Matlab的优化套件具有一个名为'Display'的属性,可以将其设置为各种值以指示所需的详细程度(请参见

I like the spirit of what you're trying to do, but I think that it probably goes against the common programming patterns in Matlab. As you correctly state, the purpose of the terminating semicolon is to supress printing of returned values. Trying to get it to incorporate your other feature might well require some deep hacking and ugly hard-to-maintain code. The standard way to implement what you describe is via property name-value pair arguments. For example, Matlab's optimization suite has a property called 'Display' that can be set to various values to indicate the desired level of verbosity (see optimset).

如果您想尝试一种方法来检查终止分号,则可以查看未记录的mintmlintmexmtree函数–

If you want to try looking for a way to check for terminating semicolons, you might look into the undocumented mint, mlintmex, and mtree functions – read more here. Unfortunately, using mlint to simply check for the "Terminate statement with semicolon to suppress output" warning (see this function on the MatlabCental File Exchange) won't work in all cases as a function call by itself doesn't produce this warning.

更新

这里是对可以插入到被调用函数中的代码的尝试,以确定调用者的行是否以分号终止.您应该意识到,这还没有经过彻底的测试,并且可能非常脆弱.我尚未使用子功能或匿名功能对其进行测试,并且我知道如果使用...将一行换成多行,则会失败.

Here's an attempt at code that could be inserted into a called function to determine if the line of the caller is terminated by a semicolon. You should be aware that this has not been thoroughly tested and is likely very fragile. I have not tested it with sub-functions or anonymous functions and I know that it fails if you wrap a line over multiple lines using ....

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

换句话说,可以尝试使用它来学习,但是我不建议您实际使用它.

In other words, play around with this to learn, but I would not recommend actually using it.

这篇关于如何确定是否在调用函数后接分号(“;")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆