Issue while converting java.lang.string to number
9 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Walter Roberson
2016-7-27
char_array = reshape( char(TheJavaString), [], 3);
new_char_array = char_array(2:end, :);
3 个评论
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,:);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!