Issue while converting java.lang.string to number

13 Ansichten (letzte 30 Tage)
Protik Das
Protik Das am 26 Jul. 2016
Kommentiert: Protik Das am 27 Jul. 2016
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 27 Jul. 2016
char_array = reshape( char(TheJavaString), [], 3);
new_char_array = char_array(2:end, :);
  3 Kommentare
Walter Roberson
Walter Roberson am 27 Jul. 2016
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 am 27 Jul. 2016
Worked perfect! Thanks a lot.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by