如何在MATLAB数据光标中以更高的精度显示数字?

时间:2011-05-11 08:01:13

标签: matlab plot precision datatip

我有精确丢失的问题。我使用以下代码将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位精度:

imprecise labels

有没有办法设定更高的精度来获得更精确的数据点?

4 个答案:

答案 0 :(得分:28)

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

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

enter image description here

如果您选择编辑文本更新功能... 选项,它将打开一个包含以下内容的默认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'),然后使用函数{打开数据游标。 {3}}并将它们设置为使用用户定义的文本更新功能。这是一个例子:

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

答案 1 :(得分:6)

如果你想永久改变 - 警告:这是对MATLAB的轻微破解 - 打开:

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

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

答案 2 :(得分:2)

不要引用我这个,但是:

1)你没有失去精度,MATLAB存储了全部值,只有显示器被削减了。

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

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

文件>偏好设置>变量编辑器

在下拉菜单中,我可以选择短,长,短e,长e,短g,长g,短eng,长eng,银行,+和老鼠。

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

答案 3 :(得分:0)

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

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