【问题标题】:How can I display numbers with higher precision in a MATLAB data cursor?如何在 MATLAB 数据游标中以更高的精度显示数字?
【发布时间】:2011-08-23 02:14:41
【问题描述】:

我有精度损失的问题。我使用以下代码将一组值从 CSV 文件导入 MATLAB 7:

function importfile(fileToRead1)
%#IMPORTFILE(FILETOREAD1)
%#  Imports data from the specified file
%#  FILETOREAD1:  file to read

DELIMITER = ',';
HEADERLINES = 0;

%# Import the file
rawData1 = importdata(fileToRead1, DELIMITER, HEADERLINES);

%# For some simple files (such as a CSV or JPEG files), IMPORTDATA might
%# return a simple array.  If so, generate a structure so that the output
%# matches that from the Import Wizard.
[~,name] = fileparts(fileToRead1);
newData1.(genvarname(name)) = rawData1;

%# Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
    assignin('base', vars{i}, newData1.(vars{i}));
end

这个非常基本的脚本只需要指定的文件:

> 14,-0.15893555 
> 15,-0.24221802
> 16,0.18478394

并将第二列转换为:

14  -0,158935550000000
15  -0,242218020000000
16  0,184783940000000

但是,如果我使用数据光标选择一个点,它只会显示 3 或 4 位精度:

有没有办法对更高的精度进行编程以获得更精确的数据点?

【问题讨论】:

    标签: matlab plot precision datatip


    【解决方案1】:

    您的 数据 没有丢失精度,数据光标显示只是没有显示完整精度,因此文本框的大小更合理。但是,如果你想提高文本数据提示中显示的精度,you can customize it

    如果您右键单击数据光标文本框,您应该会看到如下菜单:

    如果您随后选择 Edit Text Update Function... 选项,它将打开一个包含以下内容的默认 m 文件:

    function output_txt = myfunction(obj, event_obj)
    % Display the position of the data cursor
    % obj          Currently not used (empty)
    % event_obj    Handle to event object
    % output_txt   Data cursor text string (string or cell array of strings).
    
    pos = get(event_obj, 'Position');
    output_txt = {['X: ', num2str(pos(1), 4)], ...
                  ['Y: ', num2str(pos(2), 4)]};
    
    % If there is a Z-coordinate in the position, display it as well
    if length(pos) > 2
        output_txt{end+1} = ['Z: ', num2str(pos(3), 4)];
    end
    

    请注意,X 和 Y 坐标数据的文本使用num2str 格式化,第二个参数是4。这会将坐标值转换为具有 4 位精度的字符串表示形式。如果您想显示更多数字,只需增加此数字,然后将新创建的 m 文件保存到您的 path

    现在,您的数据提示文本应该更精确地显示您的数字。如果您想以编程方式完成上述所有操作,您将首先创建文本更新函数,将其保存到文件(如'updateFcn.m'),然后使用函数datacursormode 打开数据游标并将它们设置为使用您的用户定义的文本更新功能。这是一个例子:

    plot(1:10, rand(1, 10));  % Plot some sample data
    dcmObj = datacursormode;  % Turn on data cursors and return the
                              %   data cursor mode object
    set(dcmObj, 'UpdateFcn', @updateFcn);  % Set the data cursor mode object update
                                           %   function so it uses updateFcn.m
    

    【讨论】:

    • 感谢您的回答。你为我节省了很多打字时间。
    • 感谢您的精彩回答!我一直使用“将光标导出到工作区”来获得所需的精度。这是一个非常有用的技巧。
    • 这是一个很好的答案,但我是唯一一个认为这就是答案令人难以置信(以一种糟糕的方式)的人吗?
    【解决方案2】:

    如果您想进行永久性更改 - 警告:这是对 MATLAB 的一个小改动 - 打开:

    C:\Program Files\Matlab\R2007b\toolbox\matlab\graphics\@graphics\@datacursor\default_getDatatipText.m

    或类似文件,具体取决于您的版本并更改 DEFAULT_DIGITS。

    【讨论】:

      【解决方案3】:

      不要引用我的话,但是:

      1) 你并没有丢失精度,MATLAB 存储了完整的值,只是减少了显示。

      2) 在我的 MATLAB (R2009a) 版本中,我可以通过转到

      来修改命令菜单中长数字的显示方式

      文件>首选项>变量编辑器

      下拉菜单让我在 short、long、short e、long e、short g、long g、short eng、long eng、bank、+ 和 rat 之间进行选择。

      不过,我不知道这是否会影响数据光标显示的内容。

      【讨论】:

        【解决方案4】:

        您可以在脚本中添加以下内容:

        dcm_obj = datacursormode(fig);
        set(dcm_obj,'Updatefcn',@myfunction_datacursor);
        

        您需要在路径中创建并保存myfunction_datacursor 文件(通过在MATLAB 提示符下调用path 获取路径)

        function output_txt = myfunction_datacursor(obj,event_obj)
        % Display the position of the data cursor
        % obj          Currently not used (empty)
        % event_obj    Handle to event object
        % output_txt   Data cursor text string (string or cell array of strings).
        
        pos = get(event_obj,'Position');
        output_txt = {['X: ',num2str(pos(1),8)],...
                ['Y: ',num2str(pos(2),4)]};
        
        % If there is a Z-coordinate in the position, display it as well
        if length(pos) > 2
                output_txt{end+1} = ['Z: ',num2str(pos(3),8)];
        end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-29
          • 1970-01-01
          相关资源
          最近更新 更多