如何在 MATLAB 数据游标中显示更高精度的数字?
我有精度损失的问题。我使用以下代码将一组值从 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 位精度:
有没有办法编程更高的精度以获得更精确的数据点?
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:
Is there a way to program a higher precision to get more exact data points?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的数据并没有丢失精度,只是数据光标显示没有显示完整的精度,因此文本框的大小更加合理。但是,如果您想提高文本数据提示中的显示精度,请您可以自定义它。
如果右键单击数据光标文本框,您应该会看到如下所示的菜单:
如果您随后选择编辑文本更新函数...选项,它将打开一个包含以下内容的默认 m 文件:
请注意,X 和 Y 坐标数据的文本使用
num2str
,第二个参数是4
。这会将坐标值转换为 4 位精度的字符串表示形式。如果您想显示更多数字,只需增加此数字,然后将新创建的 m 文件保存在 路径。现在,您的数据提示文本应该显示更精确的数字。如果您想以编程方式完成上述所有操作,您将首先创建文本更新函数,将其保存到文件(如
'updateFcn.m'
),然后将在数据游标上使用函数datacursormode
和设置它们以使用您的用户定义的文本更新函数。这是一个例子: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:
If you then select the Edit Text Update Function... option, it will open a default m-file containing the following:
Notice that the text for the X and Y coordinate data is formatted using
num2str
, with the second argument being a4
. 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 functiondatacursormode
and set them to use your user-defined text update function. Here's an example:如果您想进行永久更改 - 警告:这是对 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.
不要引用我的话,但是:
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.
您可以在脚本中添加以下内容:
您需要创建并保存
myfunction_datacursor
文件,并在路径中添加以下内容(通过在 MATLAB 提示符中调用path
获取路径)You can add the following in your script:
You need to create and save
myfunction_datacursor
file with the following in your path (get path by callingpath
in MATLAB prompt)