开发者

How can I display numbers with higher precision in a MATLAB data cursor?

开发者 https://www.devze.com 2023-03-04 20:47 出处:网络
I have a problem with precision loss. I imported a set of values from a CSV file into MATLAB 7 using the following code:

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:lengt开发者_运维百科h(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:

How can I display numbers with higher precision in a MATLAB data cursor?

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


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:

How can I display numbers with higher precision in a MATLAB data cursor?

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


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.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消