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

发布于 2024-11-07 03:24:30 字数 1133 浏览 1 评论 0原文

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

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

I have a problem with precision loss. I imported a set of values from a CSV file into MATLAB 7 using the following code:

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

This very basic script just takes the specified file:

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

And converts the second column to:

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

However, if I select a point with the Data Cursor it only displays 3 or 4 digits of precision:

imprecise labels

Is there a way to program a higher precision to get more exact data points?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

澜川若宁 2024-11-14 03:24:30

您的数据并没有丢失精度,只是数据光标显示没有显示完整的精度,因此文本框的大小更加合理。但是,如果您想提高文本数据提示中的显示精度,请您可以自定义它

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

在此处输入图像描述

如果您随后选择编辑文本更新函数...选项,它将打开一个包含以下内容的默认 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 文件保存在 路径

现在,您的数据提示文本应该显示更精确的数字。如果您想以编程方式完成上述所有操作,您将首先创建文本更新函数,将其保存到文件(如 '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

Your data isn't losing precision, the Data Cursor display just isn't showing the full precision so that the text boxes are a more reasonable size. However, if you want to increase the precision of the display in the text datatip, you can customize it.

If you right click on a Data Cursor text box, you should see a menu like this:

enter image description here

If you then select the Edit Text Update Function... option, it will open a default m-file containing the following:

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

Notice that the text for the X and Y coordinate data is formatted using num2str, with the second argument being a 4. This converts the coordinate value to a string representation with 4 digits of precision. If you want more digits displayed, simply increase this number, then save the newly-created m-file on your path.

Now your datatip text should display more precision for your numbers. If you want to accomplish all of the above programmatically, you would first create your text update function, save it to a file (like 'updateFcn.m'), then turn on Data Cursors using the function datacursormode and set them to use your user-defined text update function. Here's an example:

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
因为看清所以看轻 2024-11-14 03:24:30

如果您想进行永久更改 - 警告:这是对 MATLAB 的轻微修改 - 打开:

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

或类似文件根据您的版本并更改 DEFAULT_DIGITS。

If you want to make a permanent change - Warning: This is a slight hack to MATLAB - open:

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

or a similar file depending on your version and change DEFAULT_DIGITS.

凉墨 2024-11-14 03:24:30

不要引用我的话,但是:

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

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

2) 在我的 MATLAB (R2009a) 版本中,我可以通过转到“文件”>“首选项”>“变量编辑器”

,其中下拉菜单允许我在短、长、短 e、长 e 之间进行选择,短g、长g、短eng、长eng、bank、+和rat。

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

Don't quote me on this, but:

1) You haven't lost precision, MATLAB stores the full value, it is only the display that has been pared down.

2) In my version of MATLAB (R2009a) I can modify the way long numbers are shown in the command menu by going to

File>Preferences>Variable Editor

where a dropdown menu lets me pick between short, long, short e, long e, short g, long g, short eng, long eng, bank, + and rat.

I have no idea whether that affects what the Data Cursor shows, though.

缱绻入梦 2024-11-14 03:24:30

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

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

You can add the following in your script:

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

You need to create and save myfunction_datacursor file with the following in your path (get path by calling path in MATLAB prompt)

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文