zoukankan      html  css  js  c++  java
  • Matlab一个错误引发的血案:??? Error using ==> str2num Requires string or character array input.

    Matlab总遇到一些神奇的问题,让人摸不着头脑。昨天编写程序的时候遇到一个让我十分火大的问题,也是自己的matlab基础不好吧。

    先描述一下问题,再GUI界面有个listbox,Tag属性是’listbox1’,里面是这样的数据,我的目的是要把这些数据转换成数值类型的矩阵:

    QQ截图20150105161932

    list_string = get(handles.listbox1,'string')
    data=str2num((list_string));

    使用上面两行代码进行转换却异常出错了!看后台的错误描述如下:

    ??? Error using ==> str2num 
    Requires string or character array input.

    Error in ==> wsy>pushbutton24_Callback at 654 
    data=str2num((list_string));

    Error in ==> gui_mainfcn at 75 
            feval(varargin{:});

    Error in ==> wsy at 16 
        gui_mainfcn(gui_State, varargin{:});

    ??? Error while evaluating uicontrol Callback.

    ??? Error using ==> feval 
    Undefined command/function 'Untitled_1_Callback'.

    Error in ==> gui_mainfcn at 75 
            feval(varargin{:});

    Error in ==> wsy at 16 
        gui_mainfcn(gui_State, varargin{:});

    ??? Error while evaluating uimenu Callback.

    Matlab抛出的异常说明str2num函数使用错误,参数必须是字符数组(char array)或者是字符串(string)。在后台看了下获得的listbox里面的数据如下:

    list_string =

        ' 56      30      3.09       0'
        ' 32      46      3.83      30'
        ' 19      48      3.91      76'
        ……(省略一大堆数据)
        ' 31     301      9.79    6634'
        ' 60     429     11.69    6935'

    对呀!尼玛难道这个不是符合要求的数据?不信我们在交互界面里面做个试验:

    str=[' 56      30      3.09       0'; ' 32      46      3.83      30'; ' 60     429     11.69    6935']

    str2num(str)

    image

    难道不应该是这样子的吗?好吧,可能不应该是数组吧,我又做了如下的实验:

    str={' 56      30      3.09       0'; ' 32      46      3.83      30'; ' 60     429     11.69    6935'}

    str2num(str)

    image

    果然报了相同的错误!在baidu和论坛里面各种查,基本上没有什么满意的答案,后来只好求助于文档:

    首先来看看str2num函数的用法:

    >> help str2num
    STR2NUM Convert string matrix to numeric array.
        X = STR2NUM(S) converts a character array representation of a matrix of
        numbers to a numeric matrix. For example,
           
             S = ['1 2'         str2num(S) => [1 2;3 4]
                  '3 4']
     
        The numbers in the string matrix S should be ASCII character
        representations of a numeric values.  Each number may contain digits,
        a decimal point, a leading + or - sign, an 'e' or 'd' preceding a
        power of 10 scale factor, and an 'i' or 'j' for a complex unit.
     
        If the string S does not represent a valid number or matrix,
        STR2NUM(S) returns the empty matrix.  [X,OK]=STR2NUM(S) will
        return OK=0 if the conversion failed.
     
        CAUTION: STR2NUM uses EVAL to convert the input argument, so side
        effects can occur if the string contains calls to functions.  Use
        STR2DOUBLE to avoid such side effects or when S contains a single
        number.

    str2num的功能是将字符串矩阵转换成数值数组,字符串必须是ASCII码表中的可转化成数值的字符,如果字符串数组不是一个有效的数字或者不能过程一个矩阵,str2num函数就会返回一个空的矩阵,[X,OK]=STR2NUM(S),如果转换失败OK=0.

    注意:str2num使用的是eval函数来转换输入的参数,所以如果字符串里面包含了函数的调用,就会产生副作用,推荐使用str2double来避免副作用(当待转换字符串矩阵S包含单个数字的时候)。

    相信很多朋友都是看了这一段文档,从此走向一条不归之路。首先我们从这段文档描述中可以获取至少三个有用的信息

    ①str2num作用的对象是‘string matrix’也就是我们的错误描述中的string or characher array.

    ②转换失败就会[X, OK] 中OK就会返回0,转换成功就会返回1(实验可得)。如下例子:

    str=['1 2 3 4'; '5 6    ']

    [X,OK]=str2num(str)

    imageimage

    ③当待转换字符数组是单个数字的时候推荐使用str2double进行转换,避免副作用,如下例子:

    Examples
       str2double('123.45e7')
       str2double('123 + 45i')
       str2double('3.14159')
       str2double('2.7i - 3.14')
       str2double({'2.71' '3.1415'})
       str2double('1,200.34')

    得到如下结果:

    1.2345e+009
    1.2300e+002 +4.5000e+001i
    3.1416
    -3.1400 + 2.7000i
    2.7100    3.1415
    1.2003e+003

    按照文档的推荐,str2num适用与转换单个数字。所以

    image转换就会失败!

    我们注意到上面的example中以这样一个例子:str2double({'2.71' '3.1415'}),看一下文档的描述:

    >> help str2double
    STR2DOUBLE Convert string to double precision value.
        X = STR2DOUBLE(S) converts the string S, which should be an
        ASCII character representation of a real or complex scalar value,
        to MATLAB's double representation.  The string may contain digits,
        a comma (thousands separator), a decimal point, a leading + or - sign,
        an 'e' preceding a power of 10 scale factor, and an 'i' for
        a complex unit.
     
        If the string S does not represent a valid scalar value, STR2DOUBLE(S)
        returns NaN.(转换失败返回NaN
     
        X = STR2DOUBLE(C) converts the strings in the cell array of strings C
        to double.  The matrix X returned will be the same size as C.  NaN will
        be returned for any cell which is not a string representing a valid
        scalar value. NaN will be returned for individual cells in C which are
        cell arrays.

    注意到我标出的红色部分,‘the strings in the cell array ’也就是说str2double还可以转换cell 类型的数据。

    但是使用str2double转换我们需要转换的数据还是不行呀:

    image

    好吧是我读文档不认真,人家都说了是转换单个数字,改成这样就可以了:

    image

    但是现在的问题是我要转换一行里面由于多个数字的数据怎么办呢?

    回到本文开头的位置:你会发现str2num的转换str只是一个‘[]’和一个‘{}’的区别就能转换了,由此可以推测他们的数据类型是不一样的,再结合str2double里面的描述可以推测‘{}’代表的就是‘cell’数据类型。

    为了验证他们的数据类型我们做如下实验:

    image

    image

    果然,他们的数据类型是不一样的!并且cell array所占的空间要大得多几乎是两倍char array的大小。

    那好了,现在的任务明确了,只需要将‘cell’类型的数据转换成数组类型就行了。

    我们看一下文档:

    Create cell array Syntax

    c = cell(n)
    c = cell(m,n) or c = cell([m n])
    c = cell(m,n,p,...) or c = cell([m n p ...])
    c = cell(size(A))
    c = cell(javaobj)


    Description

    c = cell(n) creates an n-by-n cell array of empty matrices. An error message appears if n is not a scalar.
    c = cell(m,n) or c = cell([m,n]) creates an m-by-n cell array of empty matrices. Arguments m and n must be scalars.
    c = cell(m,n,p,...) or c = cell([m n p ...]) creates an m-by-n-by-p-... cell array of empty matrices. Arguments m, n, p,... must be scalars.
    c = cell(size(A)) creates a cell array the same size as A containing all empty matrices.
    c = cell(javaobj) converts a Java array or Java object javaobj into a MATLAB cell array. Elements of the resulting cell array will be of the MATLAB type (if any) closest to the Java array elements or Java object.

    文档中描述了Cell类型的创建方式,但是我要的是转换方式呀。继续搜索文档:

    image

    貌似发现了目标:

    image

    重点来了,使用cellstr()函数可以使用character array(字符数组)创建一个cell array,使用char()可以转换回来!

    终于看到了光明,实验一下:

    image

    现在可以成功转换了!

    总结:

    使用char()函数将cell array 转换成 char array.

  • 相关阅读:
    ios常用方法
    XMPP
    ios ebooks
    uinavigationcontroller swipe back
    navigationController and ToolBar
    EMC VNX5200/5400存储 新增LUN与Hosts映射操作
    H3C交换机telnet服务认证模式配置
    配置H3C交换机ftp服务
    克隆CentOS 6.9 配置静态IP,重启网络服务时报错"eth0 does not seem to be present"
    BFS解决八数码问题和狼人过河问题
  • 原文地址:https://www.cnblogs.com/csulennon/p/4204258.html
Copyright © 2011-2022 走看看