【问题标题】:how to find negative and positive number from a string?如何从字符串中找到负数和正数?
【发布时间】:2014-02-05 23:58:58
【问题描述】:

我有一组化学反应,我只需要阅读每种化学物质的第一个数字。例如,我有一个字符串为

reaction = '-1.0CdCl2(aq)  1.0Cd++  2.0Cl-';

我想要找到 CdCl2(aq) 的 -1.0、Cd++ 的 1.0 和 Cl- 的 2.0。

【问题讨论】:

    标签: matlab pattern-matching string-matching text-processing textscan


    【解决方案1】:

    textscan 在这里工作(假设空格分隔反应物):

    >> C = textscan(reaction,'%f%s')
    C = 
        [3x1 double]    {3x1 cell}
    >> C{1}' %' decimals not shown
    ans =
        -1     1     2
    >> C{2}
    ans = 
        'CdCl2(aq)'
        'Cd++'
        'Cl-'
    

    还假设reaction 以数字开头。

    【讨论】:

    • 实际上,我有一个 excel 文件,我使用 [ndata, text, alldata] = xlsread('react.xlsx') 读取该文件。反应数据在第 2 行第 5 列中,反应 = '-1.0CdCl2(aq) 1.0Cd++ 2.0Cl-',即 [1x36 char]。我使用了 = textscan(alldata(2,5),'%f%s'),但出现错误,因为“第一个输入必须是 double 或 string 类型”。我是初学者!所以,请帮忙。谢谢
    • 我猜你知道了,但你想通过alldata{2,5}(大括号)获得该单元格的内容
    猜你喜欢
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 2015-05-14
    • 2020-01-29
    • 1970-01-01
    相关资源
    最近更新 更多