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

如何在MATLAB数据光标中以更高的精度显示数字?,matlab,plot,precision,datatip,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(

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


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

请不要引用我的话,但是:

1) 你没有失去精度,MATLAB存储了完整的值,只是显示被缩减了

2) 在我的MATLAB(R2009a)版本中,我可以通过转到修改命令菜单中显示长数字的方式

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

这里有一个下拉菜单,我可以在short、long、short e、long e、short g、long g g、short eng、long eng、bank+和rat之间进行选择

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

您的数据并没有失去精度,数据光标的显示只是没有显示全部精度,因此文本框的大小更为合理。但是,如果要提高文本数据提示中显示的精度

如果在数据光标文本框上单击鼠标右键,将看到如下菜单:

如果随后选择编辑文本更新功能…选项,它将打开包含以下内容的默认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坐标数据的文本格式为,第二个参数为
4
。这将坐标值转换为精度为4位的字符串表示形式。如果您想显示更多数字,只需增加此数字,然后将新创建的m文件保存到您的计算机上

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

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

如果要进行永久性更改-警告:这是对MATLAB的轻微攻击-打开:

C:\Program Files\Matlab\R2007b\toolbox\Matlab\graphics\@graphics\@datacursor\default\u getDatatipText.m


或类似文件,具体取决于您的版本并更改默认数字。

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

dcm_obj = datacursormode(fig);
set(dcm_obj,'Updatefcn',@myfunction_datacursor);
您需要创建并保存
myfunction\u 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