Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 450
wishi Avatar asked May 11 '11 08:05

wishi


People also ask

How do you change the precision of a number in MATLAB?

Increase Precision of ResultsIncrease precision beyond 32 digits by using digits . Find pi using vpa , which uses the default 32 digits of precision. Confirm that the current precision is 32 by using digits . Save the current value of digits in digitsOld and set the new precision to 100 digits.

How do I print more decimal places in MATLAB?

Select MATLAB > Command Window, and then choose a Numeric format option. The following table summarizes the numeric output format options. Short, fixed-decimal format with 4 digits after the decimal point.

How do I show all digits in MATLAB?

To format the way numbers display, do one of the following: On the Home tab, in the Environment section, click Preferences. Select MATLAB > Command Window, and then choose a Numeric format option. See the format reference page for a list and description of all supported numeric formats.


2 Answers

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
like image 141
gnovice Avatar answered Sep 18 '22 14:09

gnovice


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.

like image 20
Charles L. Avatar answered Sep 22 '22 14:09

Charles L.