从绘图中提高MATLAB变量/数据点的精度 [英] Increase precision of MATLAB variable/data point from a plot
问题描述
我正在绘制一个控制系统,我需要证明MATLAB的稳态误差与我的计算结果一致.
I'm plotting a control system and I need to prove that the steady state error from MATLAB coincides with my calculations.
我的计算结果给了我0.000833333
,在图上,数据提示标记的精度非常低(在我选择的位置没有小数点).
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).
但是我可以右键单击标记并选择Export cursor data to workspace...
However I can right-click the marker and select Export cursor data to workspace...
这给了我X=98.0037
,Y=98.0028
这给我一个0.0009
的错误,因此它与计算中的0.00083333
并不完全相同.我知道这是正确的,但是,我只想知道如何在可能的情况下通过绘图将变量/数据点的精度提高到小数点后4位.
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.
推荐答案
有很多不同的方法可以从这些数据提示游标中获得更高的精度:
There are many different routes to get more precisions from these data tip cursors:
正如您在问题中提到的,右键单击数据提示,然后选择Export cursor data to workspace...
.假设您导出了默认变量cursor_info
中的结构,则该结构包含3个字段:
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
是line
对象的句柄
Position
是一个1x2向量,给出了所选数据点的x
和y
坐标(如果定义了z
坐标,它将具有第三个值).
DataIndex
是用于绘制线的数组中所选数据点坐标的索引.因此,您也可以要求:
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:
第一种方法是在控制台中键入format long
.之后,您在控制台中的所有数字输出将为15位数字:
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
第二种方法是通过使用格式说明符和sprintf
或fprintf
来强制达到所需的精度:
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)以编程方式导出
除了手动导出cursor_info
数据外,您还可以使用以下代码进行调用:
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 ;
然后,您将获得与手动导出相同的cursor_info
变量.显示值的方法可以与上面相同.
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.
您还可以完全定制数据提示将显示的内容,不仅包括位置,还包括一些计算或转换后的值.右键单击数据提示,然后选择Edit text update function
.这将打开一个编辑器窗口,其中包含数据提示的当前代码,该窗口基本上会查询数据提示的位置并创建要显示的文本的单元格数组.根据需要修改此功能,然后将其保存在可以检索的位置.
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.
以我使用的数据为例,我以不同的精度显示x
和y
坐标,然后基于2个值进行计算,并且还将x
坐标转换为日期表示形式
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:
之后,在任何后续绘图或图形上,您都可以通过右键单击数据提示select text update function
,然后指向您之前保存的数据提示功能来重新应用此数据提示格式.
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.
这篇关于从绘图中提高MATLAB变量/数据点的精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!