从matlab中的字符串获取数字 [英] obtain number from string in matlab

查看:423
本文介绍了从matlab中的字符串获取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  dd = {'L','temp1','temp23',' REAS'}; 

我想从包含任何数字的字符串中提取数字(如果有意义的话)。所以,这个问题的解决方案应该是1和23。

如何在matlab中实现这个功能?

解决方案

这是解决方案的一部分。假设

  myString ='temp23'

然后表达式

$ $ $ $ $ $ $ $ $ str2double(a(isstrprop(a,'digit')))

会返回

  23 

我没有时间把它变成一个函数来处理与您的字符串数组,但这应该让你开始。

@DennisJahruddin建议我的答案以下完成。

  dd = {'L','temp1','temp23','Reas' }; 
ee = cellfun(@(a)str2double(a(isstrprop(a,'digit'))),dd);
ff = ee(〜isnan(ee))


I have an array of strings:

dd = {'L','temp1','temp23','Reas'};

I would like to extract the numbers from the strings that contain any numbers (if that makes sense). So, the solution for this question should be 1 and 23.

How can I achieve this in matlab?

解决方案

Here's part of the solution. Suppose

myString = 'temp23'

then the expression

str2double(a(isstrprop(a,'digit')))

will return

23

I haven't got the time to turn this into a function to deal with your array of strings but this should get you started.

@DennisJahruddin suggested the following completion of my answer. I haven't tested it thoroughly:

dd = {'L','temp1','temp23','Reas'};
ee = cellfun(@(a) str2double(a(isstrprop(a,'digit'))),dd);
ff = ee(~isnan(ee))

这篇关于从matlab中的字符串获取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆