Matlab一個(gè)錯(cuò)誤引發(fā)的血案:??? Error using ==> str2num Requires string or character array input.【轉(zhuǎn)發(fā)】

2017-05-25  by:CAE仿真在線  來源:互聯(lián)網(wǎng)

Matlab總遇到一些神奇的問題,讓人摸不著頭腦。昨天編寫程序的時(shí)候遇到一個(gè)讓我十分火大的問題,也是自己的matlab基礎(chǔ)不好吧。

先描述一下問題,再GUI界面有個(gè)listbox,Tag屬性是’listbox1’,里面是這樣的數(shù)據(jù),我的目的是要把這些數(shù)據(jù)轉(zhuǎn)換成數(shù)值類型的矩陣:

QQ截圖20150105161932

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

使用上面兩行代碼進(jìn)行轉(zhuǎn)換卻異常出錯(cuò)了!看后臺(tái)的錯(cuò)誤描述如下:

??? 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函數(shù)使用錯(cuò)誤,參數(shù)必須是字符數(shù)組(char array)或者是字符串(string)。在后臺(tái)看了下獲得的listbox里面的數(shù)據(jù)如下:

list_string =

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

對(duì)呀!尼瑪難道這個(gè)不是符合要求的數(shù)據(jù)?不信我們?cè)诮换ソ缑胬锩孀鰝€(gè)試驗(yàn):

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

str2num(str)

image

難道不應(yīng)該是這樣子的嗎?好吧,可能不應(yīng)該是數(shù)組吧,我又做了如下的實(shí)驗(yàn):

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

str2num(str)

image

果然報(bào)了相同的錯(cuò)誤!在baidu和論壇里面各種查,基本上沒有什么滿意的答案,后來只好求助于文檔:

首先來看看str2num函數(shù)的用法:

>> 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的功能是將字符串矩陣轉(zhuǎn)換成數(shù)值數(shù)組,字符串必須是ASCII碼表中的可轉(zhuǎn)化成數(shù)值的字符,如果字符串?dāng)?shù)組不是一個(gè)有效的數(shù)字或者不能過程一個(gè)矩陣,str2num函數(shù)就會(huì)返回一個(gè)空的矩陣,[X,OK]=STR2NUM(S),如果轉(zhuǎn)換失敗OK=0.

注意:str2num使用的是eval函數(shù)來轉(zhuǎn)換輸入的參數(shù),所以如果字符串里面包含了函數(shù)的調(diào)用,就會(huì)產(chǎn)生副作用,推薦使用str2double來避免副作用(當(dāng)待轉(zhuǎn)換字符串矩陣S包含單個(gè)數(shù)字的時(shí)候)。


相信很多朋友都是看了這一段文檔,從此走向一條不歸之路。首先我們從這段文檔描述中可以獲取至少三個(gè)有用的信息

①str2num作用的對(duì)象是‘string matrix’也就是我們的錯(cuò)誤描述中的string or characher array.

②轉(zhuǎn)換失敗就會(huì)[X, OK] 中OK就會(huì)返回0,轉(zhuǎn)換成功就會(huì)返回1(實(shí)驗(yàn)可得)。如下例子:

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

[X,OK]=str2num(str)

image
image

③當(dāng)待轉(zhuǎn)換字符數(shù)組是單個(gè)數(shù)字的時(shí)候推薦使用str2double進(jìn)行轉(zhuǎn)換,避免副作用,如下例子:

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

得到如下結(jié)果:

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

按照文檔的推薦,str2num適用與轉(zhuǎn)換單個(gè)數(shù)字。所以

image
轉(zhuǎn)換就會(huì)失敗!

我們注意到上面的example中以這樣一個(gè)例子: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.(轉(zhuǎn)換失敗返回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.

注意到我標(biāo)出的紅色部分,‘the strings in the cell array ’也就是說str2double還可以轉(zhuǎn)換cell 類型的數(shù)據(jù)。

但是使用str2double轉(zhuǎn)換我們需要轉(zhuǎn)換的數(shù)據(jù)還是不行呀:

image

好吧是我讀文檔不認(rèn)真,人家都說了是轉(zhuǎn)換單個(gè)數(shù)字,改成這樣就可以了:

image

但是現(xiàn)在的問題是我要轉(zhuǎn)換一行里面由于多個(gè)數(shù)字的數(shù)據(jù)怎么辦呢?

回到本文開頭的位置:你會(huì)發(fā)現(xiàn)str2num的轉(zhuǎn)換str只是一個(gè)‘[]’和一個(gè)‘{}’的區(qū)別就能轉(zhuǎn)換了,由此可以推測他們的數(shù)據(jù)類型是不一樣的,再結(jié)合str2double里面的描述可以推測‘{}’代表的就是‘cell’數(shù)據(jù)類型。

為了驗(yàn)證他們的數(shù)據(jù)類型我們做如下實(shí)驗(yàn):

image

image

果然,他們的數(shù)據(jù)類型是不一樣的!并且cell array所占的空間要大得多幾乎是兩倍char array的大小。

那好了,現(xiàn)在的任務(wù)明確了,只需要將‘cell’類型的數(shù)據(jù)轉(zhuǎn)換成數(shù)組類型就行了。

我們看一下文檔:

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類型的創(chuàng)建方式,但是我要的是轉(zhuǎn)換方式呀。繼續(xù)搜索文檔:

image

貌似發(fā)現(xiàn)了目標(biāo):

image

重點(diǎn)來了,使用cellstr()函數(shù)可以使用character array(字符數(shù)組)創(chuàng)建一個(gè)cell array,使用char()可以轉(zhuǎn)換回來!

終于看到了光明,實(shí)驗(yàn)一下:

image

現(xiàn)在可以成功轉(zhuǎn)換了!



總結(jié):

使用char()函數(shù)將cell array 轉(zhuǎn)換成 char array.

轉(zhuǎn)自:http://www.cnblogs.com/csulennon/p/4204258.html


開放分享:優(yōu)質(zhì)有限元技術(shù)文章,助你自學(xué)成才

相關(guān)標(biāo)簽搜索:Matlab一個(gè)錯(cuò)誤引發(fā)的血案:??? Error using ==> str2num Requires string or character array input.【轉(zhuǎn)發(fā)】 MatLab培訓(xùn) MatLab培訓(xùn)課程 MatLab在線視頻教程 MatLab技術(shù)學(xué)習(xí)教程 MatLab軟件教程 MatLab資料下載 MatLab代做 MatLab基礎(chǔ)知識(shí) Fluent、CFX流體分析 HFSS電磁分析 Ansys培訓(xùn) Abaqus培訓(xùn) 

編輯
在線報(bào)名:
  • 客服在線請(qǐng)直接聯(lián)系我們的客服,您也可以通過下面的方式進(jìn)行在線報(bào)名,我們會(huì)及時(shí)給您回復(fù)電話,謝謝!
驗(yàn)證碼

全國服務(wù)熱線

1358-032-9919

廣州公司:
廣州市環(huán)市中路306號(hào)金鷹大廈3800
電話:13580329919
          135-8032-9919
培訓(xùn)QQ咨詢:點(diǎn)擊咨詢 點(diǎn)擊咨詢
項(xiàng)目QQ咨詢:點(diǎn)擊咨詢
email:kf@1cae.com