Issue while converting java.lang.string to number

조회 수: 10 (최근 30일)
Protik Das
Protik Das 2016년 7월 26일
댓글: Protik Das 2016년 7월 27일
Hello everyone!
I have a code that reads part of a xml file using getTextContent method. The output data is a java.lang.String type string. This is usually a N×3 looking string on which I do str2num() to make a N×3 matrix. So far it worked without any issue. But I came to a problem while my data format was like this:
val =
-4.5600 ********** 201.2678
-4.5594 37.8202 201.2920
-4.5587 37.8767 201.3163
-4.5581 37.9339 201.3406
-4.5574 37.9918 201.3649
-4.5568 38.0504 201.3892
-4.5562 38.1098 201.4136
-4.5555 38.1698 201.4380
-4.5549 38.2305 201.4625
Now asterisk (*) being a special character in Matlab, while I do str2num() on the string it returns an empty matrix. The first line is not so important, so I tried to get rid of the first line. As it is a java string, I failed to do it. I tried converting it to char by doing char(val) but it becomes 1×(N*3) char which makes it hard to process.
I would like to get rid of the first line & have the java string converted to a (N-1)×3 matrix. Any clue? The string I am trying to convert is uploaded as a mat file here.
I understand xml2struct can convert XML data directly to numbers, but my whole code is written using xmlread() so I cannot go back and rewrite everything due to this problem.
Any help is really appreciated.

채택된 답변

Walter Roberson
Walter Roberson 2016년 7월 27일
char_array = reshape( char(TheJavaString), [], 3);
new_char_array = char_array(2:end, :);
  댓글 수: 3
Walter Roberson
Walter Roberson 2016년 7월 27일
Your array is not an N x 3 looking string: it is a vector of data separated by newlines.
temp = textscan(char(data),'%f%f%f', 'TreatAsEmpty', '**********', 'CollectOutput', 1);
val = temp{1};
The result will be a 4001x3 array of double. If you do not want the first line then you can
val = temp{1}(2:end,:);
Protik Das
Protik Das 2016년 7월 27일
Worked perfect! Thanks a lot.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by