krb686
krb686

Reputation: 1796

Increase precision of MATLAB variable/data point from a plot

I'm plotting a control system and I need to prove that the steady state error from MATLAB coincides with my calculations.

My calculations have given me 0.000833333, and on the plot, the data-tip markers have very little precision (no decimal points at the location I selected).

However I can right-click the marker and select Export cursor data to workspace...

This gives me the position location of X=98.0037, Y=98.0028

That gives me an error of 0.0009, so it's not quite the 0.00083333 from calculations. I know this is correct however, I just would like to know how to increase the precision of the variables/data points from plot past 4 decimal places if possible.

enter image description here

Upvotes: 1

Views: 6609

Answers (1)

Hoki
Hoki

Reputation: 11792

There are many different routes to get more precisions from these data tip cursors:

1) Export manually:

As you mentioned in your question, right click on the datatip then select Export cursor data to workspace.... Let's assume you export that in the default variable cursor_info, you then get a structure with 3 fields:

cursor_info = 
       Target: 492.0040
     Position: [7.3593e+05 10.6353]
    DataIndex: 7

Target is the handle of the line object to which the cursor was snapped
Position is a 1x2 vector giving the x and y coordinates of the selected data points (it will have a third value if the z coordinates are defined)
DataIndex is the index of the selected data point coordinates in the array that was used to plot the line. So you could also ask for :

>> x(7) %// or "x(cursor_info.DataIndex)" would be the same
ans =
   7.3593e+05
>> y(7)
ans =
   11.3200

Now by default the display of values in the console is limited to a few digits, but the number in the variable has more precision. 2 ways to display more precision:

First method is to type format long in the console. After that all your numeric output in the console will be with 15 digits:

>> cursor_info.Position
ans =
   1.0e+05 *
   7.359289702215524   0.000106353183026

Second method is to force the precision you desire by using a format specifier and sprintf or fprintf:

>> fprintf('x=%15.15d y=%g \n',cursor_info.Position)
x=7.359289702215524e+05 y=10.6353

2) Export programatically

Alternatively to the manual export of the cursor_info data, you can call that with code:

dcm = datacursormode(gcf) ;
cursor_info = dcm.getCursorInfo ;

You then get the same cursor_info variable than if you exported it manually. Displaying the values can be done the same way than above.

3) Direct display (at the wanted precision).

You can also achieve a full customization of what the data tip will display, not only the position but also some calculated or converted values. Right click on the data tip and select Edit text update function. This will open an editor window with the current code for the data tip, which basically query the data tip position and create a cell array of text to be displayed. Modify this function to your needs then save it somewhere you can retrieve it.

For an example with the data I was using, I display the x and y coordinates with different precision, then a calculation based on the 2 values, and I also convert the x coordinates to the date representation.

function output_txt = myModifiedDatatip(obj,event_obj)

pos = get(event_obj,'Position');
output_txt = {['X: ',num2str(pos(1),12)],... %// default code, only the output precision is changed
              ['Y: ',num2str(pos(2),8)]};

%// add all the informations you want to calculate and display directly here:
output_txt{end+1} = ['error (y-x): ',sprintf('%16f', pos(2)-pos(1))]; %// difference between x and x
output_txt{end+1} = ['Date: ', datestr(pos(1))]; %// display the date/time 

This example will display a data tip like so: datatip

After that, on any subsequent plot or figure, you can re-apply this data tip format by right clicking on a data tip, select text update function, then point to the data tip function you saved earlier.

Upvotes: 4

Related Questions