在编辑器中自定义数据提示 [英] Customizing data tips in editor

查看:139
本文介绍了在编辑器中自定义数据提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是一个确切的问题,因为我有答案,但是由于我找不到有关它的消息来源,因此我会将问题和答案一起发布在这里,以防您感兴趣. (而且如果我忘记了我是怎么做的,也能够再次找到它.)

This is not exactly a question, because i have the answer, but as I couldn't find sources about it, i'll post the question along with the answer here in case it is of some interest for you. (And also to be able to find it again if i forget about how i did it).

我在查看一些代码时遇到了一个问题.大量变量没有在代码中进行解释,每次我想知道变量的含义时,我都必须在数据文件中查找和/或从代码中执行的操作中猜测它们的含义.

I came across a problem while reviewing some code. The huge amount of variables were not explained in the code and everytime I wanted to know the meaning of a variable I had to look in the Data files and/or guess their meaning from the operations that were done in the code.

不用说这很浪费时间,我已经寻找了减少浪费时间的最佳方法.

No need to say that this was pretty time consuming and i've searched the best way to lose less time.

首先,我将有关变量的所有解释都放在注释中,问题是它在脚本中添加了很多行.

First of all, I put all the explanations about the variables in comments, the problem being it's adding a (huge!) lot of lines in the script.

我还在MATLAB中使用在编辑模式下启用数据提示".当您将鼠标悬停在已经评估过的变量名称上时,MATLAB会为您提供所有尺寸的大小以及至少它的第一个值,从而使您更容易知道正在操作的对象. (请参见下图)

I'm also using the "Enable data tips in edit mode" in MATLAB. When you hover over a variable name MATLAB already evaluated, it gives you it's size along all dimensions and at least its first values, making it already easier to know what objects you are manipulating. (see image below)

我想到的自然想法是:是否可以使MATLAB在数据提示中显示更多自定义信息?

The natural idea that came to my mind was the following : would it be possible to make MATLAB display some more custom informations in the data tips?

答案是肯定的!

查看答案中的代码

推荐答案

我们将需要进行一些预处理才能使其正常工作,即:

We will need to do some preprocessing in order for this to work, i.e. :

1)创建一个数据文件,将变量名链接到它们的描述 (这是无聊的部分,尽管我已经必须这样做才能理解代码.每次遇到新变量时,只需添加一行即可)

1) Create a data file linking the variable names to their description (This is the boring part, although i already had to do it in order to understand the code. Just add a line everytime you come across a new variable)

我选择将这些数据保存在CSV文件中,其中第一列包含变量名称,第二列包含描述,例如

I chose to save these datas in a CSV file, where the first column contains the variable names and the second one contains the descriptions, e.g.

2)编辑MATLAB的函数datatipinfo(可通过在MATLAB的命令窗口中键入edit datatipinfo来访问其内部代码)

2) Edit MATLAB's function datatipinfo (It's inner code can be accessed by typing edit datatipinfo in MATLAB's command window)

datatipinfo函数如下所示:

function datatipinfo(val)

% Some error checking / Initialization


   function prefix=sizeType %#ok<DEFNU> All uses are in EVALC calls.
        s = size(val);
        D = numel(s);
        if D == 2
            theSize = [num2str(s(1)), 'x', num2str(s(2))];
        elseif D == 3
            theSize = [num2str(s(1)), 'x', num2str(s(2)), 'x', ...
                num2str(s(3))];
        else
            theSize = [num2str(D) '-D'];
        end



        if isempty(val) == 0
            prefix = [name ': ' theSize ' ' class(val)];
        else
            prefix = [name ': empty ' theSize ' ' class(val)];
        end

     end

% Some other stuff

end

这是prefix函数,我们将对其进行编辑以执行我们想要的操作,并在初始化阶段进行一些文件扫描和字符串比较:

And it's the prefix function that we'll edit in order to do what we want to do, along with some filescanning and string comparison in the initialization phase :

A)初始化阶段:

% Read data from csv file :
fid = fopen('ToyVars.csv');  
Data = textscan(fid, '%s%s','whitespace','','delimiter',';'); 
fclose(fid);

B)将要悬停的变量的名称与数据中的变量的名称进行比较

NameFound=0;
% Get Variable Names and Corresponding comments     
TmpNames=Data{1,1};
TmpComments=Data{1,2};

% Loop through TmpNames. If a Name matches, assign corresponding comment to the variable Comment

for ii=1:size(TmpNames,1)

   if(isequal(char(TmpNames(ii))),name)

       Comment=char(TmpComments(ii));
       NameFound=1;

   end

end

C)如果NameFound==1

C) Add the comment in the datatip if NameFound==1

if NameFound

    if isempty(val) == 0
        prefix = [name ': ' theSize ' ' class(val) ' : ' Comment];
    else
        prefix = [name ': empty ' theSize ' ' class(val)  ' : ' Comment];
    end

else 

    if isempty(val) == 0
         prefix = [name ': ' theSize ' ' class(val)];
    else
         prefix = [name ': empty ' theSize ' ' class(val) ];
    end

end

瞧瞧!

这篇关于在编辑器中自定义数据提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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