str2num() not working on bigger arrays?

11 ビュー (過去 30 日間)
Marco 2014 年 2 月 2 日
編集済み: Marco 2014 年 2 月 4 日
Hello, I tried the following, and it is not working:
>> whos Name Size Bytes Class Attributes
XYZasCHARarray 121704x24 5841792 char
>> conversionstep1=str2num(XYZasCHARarray)
conversionstep1 =
>> whos Name Size Bytes Class Attributes
XYZasCHARarray 121704x24 5841792 char
conversionstep1 0x0 0 double
If I proceed on a smaller array (74208x24 char) for testing of my code, this step works fine and produces me the resulting array (74208x3 double) correctly. I so far have no reason to assume that the syntax inside my big char array would be different from the syntax in the smaller char array. Both arrays are the output of my parser, and this parser so far worked without problems, and the input data comes from a huge public scientific database wherein all datafiles should be correctly formated. In a text editor also the big input files looks o.k. . But I finally cannot confirm the big char variable in MATLAB to indeed be o.k. , because the Workspace Variable Browser does not allow to see the content of that array, because it is so big.
Is there a limitation in the use of the str2num() function, or is it a bug? What can I do to get my data converted? Thanks for help!

回答 (2 件)

Walter Roberson
Walter Roberson 2014 年 2 月 2 日
You should be considering using str2double() instead of str2num(). str2num() requires that the strings be executed as commands.
Note: str2double() works on a char vector or a cell array of strings, but not on a char array.
  1 件のコメント
Marco 2014 年 2 月 4 日
編集済み: Marco 2014 年 2 月 4 日
Once the syntax in the input file became fixed (see my answer added below), there was no need to change from str2num() to any other construct of functions. The str2num() worked fine, then.
Anyway, thanks for your help, Mr. Roberson!
By the way, I do not understand what would make the difference between the str2num() and str2double() functions. To me both appear to do the same job, concerning the result. Is there any advantage in processing speed or stability, or why did you immediately thought about suggesting me to better use that function? I am just asking for learning something...


Marco 2014 年 2 月 4 日
I found what caused the problem. There is – of course - no problem with the str2num() function. The problem instead - and as to be expected - is once more the user: My input data surprisingly didn´t match the specification of syntax, as it was supposed to respect it. Screening all the many lines in the really big file in a text editor, I found that some values in the input file are so big, that they became wider than the space reserved for their digits in the fixed field text file allowed for. Due to such width overflow subsequent columns in such lines became shifted to the right and my parser wasn´t prepared to catch such syntax violation. (My parser does not work with delimiters, because there are no delimiters available in the file. It really has to pick data from the correct column in the string). As a result of the syntax violation the str2num() function found nonsense strings which can not be converted to any number. Thus, I received the empty array.


Help Center および File ExchangeData Type Conversion についてさらに検索


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by