how to remove uncertainty from data in order to plot it

2 次查看(过去 30 天)
Hi,
I have a dataset consisting of a array of strings with a number and uncertainty
e.g."2.19479E-8 ± 2.46286E-10"
I want to just get the first value which would be 2.19479E-8 in this example. Does anyone know how to do this?
Cheers

回答(2 个)

John D'Errico
John D'Errico 2022-2-26
编辑:John D'Errico 2022-2-26
You could do something as simple as:
S = "2.19479E-8 ± 2.46286E-10";
substrs = split(S);
N = double(substrs(1))
N = 2.1948e-08
That is, break up the string into pieces, using the space to indicate where the split occurs. Then grab the first of those pieces. There are certainly other more sophisticated ways, but simple is often good.
I might remind you that it is a bad idea to just forget about that uncertainty. So better could be to also extract that uncertainty in the same way. Now instead of using plot to display the results, you could use a tool like the errorbar plotting tools, to plot not only the central value, but display the upper and lower limits on those central values. This would be a far more valuable plot.
help errorbar
ERRORBAR Plot error bars along curve ERRORBAR(Y,E) plots Y and draws a vertical error bar at each element of Y. The error bar is a distance of E(i) above and below the curve so that each bar is symmetric and 2*E(i) long. ERRORBAR(X,Y,E) plots Y versus X with symmetric vertical error bars 2*E(i) long. X, Y, E must be the same size. When they are vectors, each error bar is a distance of E(i) above and below the point defined by (X(i),Y(i)). When they are matrices, each error bar is a distance of E(i,j) above and below the point defined by (X(i,j),Y(i,j)). ERRORBAR(X,Y,NEG,POS) plots X versus Y with vertical error bars NEG(i)+POS(i) long specifying the lower and upper error bars. X and Y must be the same size. NEG and POS must be the same size as Y or empty. When they are vectors, each error bar is a distance of NEG(i) below and POS(i) above the point defined by (X(i),Y(i)). When they are matrices, each error bar is a distance of NEG(i,j) below and POS(i,j) above the point defined by (X(i,j),Y(i,j)). When they are empty the error bar is not drawn. ERRORBAR( ___ ,Orientation) specifies the orientation of the error bars. Orientation can be 'horizontal', 'vertical', or 'both'. When the orientation is omitted the default is 'vertical'. ERRORBAR(X,Y,YNEG,YPOS,XNEG,XPOS) plots X versus Y with vertical error bars YNEG(i)+YPOS(i) long specifying the lower and upper error bars and horizontal error bars XNEG(i)+XPOS(i) long specifying the left and right error bars. X and Y must be the same size. YNEG, YPOS, XNEG, and XPOS must be the same size as Y or empty. When they are empty the error bar is not drawn. ERRORBAR( ___ ,LineSpec) specifies the color, line style, and marker. The color is applied to the data line and error bars. The line style and marker are applied to the data line only. ERRORBAR(AX, ___ ) plots into the axes specified by AX instead of the current axes. H = ERRORBAR( ___ ) returns handles to the errorbarseries objects created. ERRORBAR creates one object for vector input arguments and one object per column for matrix input arguments. Example: Draws symmetric error bars of unit standard deviation. x = 1:10; y = sin(x); e = std(y)*ones(size(x)); errorbar(x,y,e) Documentation for errorbar doc errorbar Other functions named errorbar embedded.fi/errorbar

Voss
Voss 2022-2-26
编辑:Voss 2022-2-26
str = "2.19479E-8 ± 2.46286E-10";
% to get a new string with just the first value:
new_str = extractBefore(str," ±")
new_str = "2.19479E-8"
% or to get the numeric value itself:
new_value = str2double(extractBefore(str," ±"))
new_value = 2.1948e-08
  2 个评论
John D'Errico
John D'Errico 2022-2-26
Good point. The string tools have been greatly improved over the years, and I did not notice the extractBefore utility in my quick glance through the available methods.
Voss
Voss 2022-2-26
I had to go searching for it myself. I'm used to working with character arrays, and my first thought here was to index into that string like it was a character array, but obviously that gave me an error. My next impulse was to convert the string into a character array and then index into that, but I figured there was probably a better way, so I went looking around and found extractBefore.
And from your answer I learned that double() (as an alternative to str2double()) can be called on a string. I did not know that!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by