Matlab always encounter some magical problems, let people feel the mind. Yesterday when writing a program encountered a let me very fire big problem, but also their own MATLAB Foundation is not good bar.
First describe the problem, then the GUI interface has a Listbox,tag attribute is ' listbox1 ', inside is such data, my purpose is to convert this data into a matrix of numeric types:
list_string = Get (Handles.listbox1, ' string ')
Data=str2num ((list_string));
Using the above two lines of code for the conversion, but an abnormal error! Look at the background error description as follows:
??? 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 throws an exception stating that the Str2Num function uses an error, that the argument must be a character array (char array) , or a string. In the background look at the data obtained in the ListBox is as follows:
List_string =
' 56 30 3.09 0 '
' 32 46 3.83 30 '
' 19 48 3.91 76 '
...... (Omit a large pile of data)
' 31 301 9.79 6634 '
' 60 429 11.69 6935 '
Yes! Is this not the data that meets the requirements? Don't believe us in the interactive interface to do an experiment:
str=[' 56 30 3.09 0 '; ' 32 46 3.83 30 '; ' 60 429 11.69 6935 ']
Str2Num (str)
Isn't it supposed to be like this? Well, maybe it shouldn't be an array, I did the following experiment:
str={' 56 30 3.09 0 '; ' 32 46 3.83 30 '; ' 60 429 11.69 6935 '}
Str2Num (str)
The same mistake was reported! In Baidu and the Forum in a variety of checks, basically no satisfactory answer, then had to resort to documents:
Let's take a look at the use of the Str2Num function:
>> 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 is 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 ten scale factor, and a ' 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) would
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 if S contains a single
Number.
The function of Str2Num is to convert a string matrix into a numeric array, and the string must be a character in the ASCII code table that can be converted to a numeric value , if the string array is not a valid number or a matrix cannot be process. The Str2Num function returns an empty matrix, [X,ok]=str2num (S) If the conversion fails ok=0.
Note: Str2Num uses the Eval function to convert the input parameters, so if the string contains a function call, it will have side effects, it is recommended to use str2double to avoid side effects (when the string matrix S containing a single number to be converted).
Believe that a lot of friends are looking at this piece of documentation, from then on to a way not to return. First, we can get at least three useful information from this document description
The object of the ①str2num action is ' string matrix ', which is the string in our error description or Characher Array.② conversion failure will return 0 in [X, OK], and the conversion will return 1 (experiment available). Here's an example:
Str=[' 1 2 3 4 '; ' 5 6 ']
[X,ok]=str2num (str)
③ when converting a character array to a single number, it is recommended to use str2double to avoid side effects, as in the following example:
Examples
Str2double (' 123.45e7 ')
Str2double (' 123 + 45i ')
Str2double (' 3.14159 ')
Str2double (' 2.7i-3.14 ')
str2double ({' 2.71 ' 3.1415 '})
Str2double (' 1,200.34 ')
The following results are obtained:
1.2345e+009
1.2300e+002 +4.5000e+001i
3.1416
-3.1400 + 2.7000i
2.7100 3.1415
1.2003e+003
As recommended by the documentation, Str2Num applies and converts a single number. So
The conversion will fail!
We noticed in the above example in such an example:str2double ({' 2.71 ' 3.1415 '}), take a look at the description of the document:
>> Help Str2double
Str2double Convert string to double precision value.
X = str2double (s) converts the string S, which should is 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 ten scale factor, and an ' I ' for
A complex unit.
If The string S does not represent a valid scalar value, str2double (S)
Returns NaN. (conversion failure Returns Nan)
X = str2double (C) convertsThe strings in the cell arrayof strings C
to double. The matrix X returned would be the same size as C. NaN would
be returned for any cell which are not a string representing a valid
Scalar value. NaN'll be returned-individual cells in C which is
Cell arrays.
Notice the red Part I marked, ' the strings in thecell array ' which means that str2double can also convert data of the cell type.
But using str2double to convert the data we need to convert is still not working:
Okay, I read the document. Not seriously, people have said is to convert a single number, change to this can be:
But now the question is, what do I do if I want to convert a line of data because of multiple numbers?
Back to the beginning of this article: you will find that the Str2Num conversion str is only a '[]' and a '{}' difference can be converted, which can be inferred that their data type is not the same, and then combined with str2double inside the description can be inferred ' {} ' represents the ' cell ' data type.
To verify their data types, we do the following experiments:
Sure enough, their data types are not the same! and the cell array occupies a much larger space that is almost twice times the size of the char array.
Well, now the task is clear, just need to convert the 'cell' type data into an array type.
Let's take a look at the documentation:
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 would be a of the MATLAB type (if any) closest to the Java array Elements or Java object .
The way the cell type was created is described in the documentation, but I want the conversion method. Continue searching for documents:
Seems to have found the target:
The point is that with the CELLSTR () function you can use the character array (character array) to create a cell array, which can be converted back using char ()!
Finally see the light, experiment:
Now you can convert it successfully!
Take a look at the overall effect of a software completed in the last two days and have time to open it up and write a step by step:
Summarize:
Use the char () function to convert the cell array to a char array.
MATLAB a mistake caused by the massacre:??? The Error using ==> str2num Requires string or character array input.