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
0 件のコメント
回答 (2 件)
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))
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
0 件のコメント
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," ±")
% or to get the numeric value itself:
new_value = str2double(extractBefore(str," ±"))
2 件のコメント
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
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 Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!