我有精度损失的问题。我使用以下代码将 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 位精度:
有没有一种方法可以编程更高的精度以获得更精确的数据点?
最佳答案
您的数据并没有失去精度,只是数据游标显示没有显示完整的精度,因此文本框的大小更合理。但是,如果您想提高文本数据提示中显示的精度,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
关于matlab - 如何在 MATLAB 数据游标中以更高精度显示数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5961034/