从细胞阵列中删除测量单元

发布于 2025-02-07 06:27:25 字数 482 浏览 2 评论 0原文

我正在尝试将单元从单元阵列数据的一列中删除,即:

cArray =
time                 temp
2022-05-10 20:19:43  '167 °F'
2022-05-10 20:19:53  '173 °F'
2022-05-10 20:20:03  '177 °F'
...
2022-06-09 20:18:10  '161 °F'

我尝试了str2double,但要获取所有NAN。

我在Regexp上找到了一些信息,但并不完全遵循示例不完全相同。

任何人都可以帮助我获取温度列以仅阅读值,即:

cArray =
time                 temp
2022-05-10 20:19:43  167
2022-05-10 20:19:53  173
2022-05-10 20:20:03  177
...
2022-06-09 20:18:10  161

I am trying to remove the units out of a column of cell array data i.e.:

cArray =
time                 temp
2022-05-10 20:19:43  '167 °F'
2022-05-10 20:19:53  '173 °F'
2022-05-10 20:20:03  '177 °F'
...
2022-06-09 20:18:10  '161 °F'

I have tried str2double but get all NaN.

I have found some info on regexp but don't follow exactly as the example is not the same.

Can anyone help me get the temp column to only read the value i.e.:

cArray =
time                 temp
2022-05-10 20:19:43  167
2022-05-10 20:19:53  173
2022-05-10 20:20:03  177
...
2022-06-09 20:18:10  161

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捂风挽笑 2025-02-14 06:27:25

对于某些单元格数据阵列,

cArray = { ...
  1,   '123 °F'
  2,   '234 °F'
  3,   '345 °F'
  };

最简单的选项是,如果我们可以安全地假设温度数据始终以数字值开头,并且您需要所有数字值。然后,我们可以使用REGEX仅匹配

temps = regexp( cArray(:,2), '\d+', 'match', 'once' );

Match选项原因REGEXP以返回匹配字符串而不是匹配索引,而a a a in Code>一旦意思是“停在第一场比赛中”,这样我们就会忽略第一个非数字角色之后的所有内容。

模式'\ d+'表示“一个或多个数字”。您可以使用'\ d+(\。\ d+)?将其扩展到与十进制零件匹配的数字,如果是要求。

然后,如果要实际输出数字,则应使用str2double。您可以在循环中执行此操作,也可以使用cellfun,这是实现同一件事的一种紧凑方法。

temps = cellfun( @str2double, temps, 'uni', 0 ); % 'uni'=0 to retain cell array

最后,您可以在Carray中覆盖列

cArray(:,2) = temps;

For some cell array of data

cArray = { ...
  1,   '123 °F'
  2,   '234 °F'
  3,   '345 °F'
  };

The easiest option is if we can safely assume the temperature data always starts with numeric values, and you want all of the numeric values. Then we can use regex to match only numbers

temps = regexp( cArray(:,2), '\d+', 'match', 'once' );

The match option causes regexp to return the matching string rather than the index of the match, and once means "stop at the first match" so that we ignore everything after the first non-numeric character.

The pattern '\d+' means "one or more numbers". You could expand it to match numbers with a decimal part using '\d+(\.\d+)?' instead if that's a requirement.

Then if you want to actually output numbers, you should use str2double. You could do this in a loop, or use cellfun which is a compact way of achieving the same thing.

temps = cellfun( @str2double, temps, 'uni', 0 ); % 'uni'=0 to retain cell array

Finally you can override the column in cArray

cArray(:,2) = temps;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文