How to extract numeric values from char

22 views (last 30 days)
I have 9*1 column cell C. Here is the contents:
{'{"name":"rect","x":101,"y":30,"width":239,"height":244}'}
{'{"name":"rect","x":503,"y":88,"width":124,"height":165}'}
{'{"name":"rect","x":123,"y":78,"width":93,"height":111}' }
{'{"name":"rect","x":386,"y":47,"width":105,"height":112}'}
{'{"name":"rect","x":582,"y":40,"width":100,"height":113}'}
{'{"name":"rect","x":169,"y":50,"width":187,"height":209}'}
{'{"name":"rect","x":563,"y":49,"width":131,"height":181}'}
{'{"name":"rect","x":414,"y":128,"width":47,"height":53}' }
{'{"name":"rect","x":315,"y":131,"width":26,"height":33}' }
Each cell reprsents a rectangular bounding box defineds by its top-left corner coordinate and width and height. Then I want to extract from the 9*1 cell array these bounding box information (x, y, w, h) into a numeric matrix 9*4. How to do so?

Accepted Answer

Stephen23
Stephen23 on 25 Jan 2020
>> C = {'{"name":"rect","x":101,"y":30,"width":239,"height":244}';
'{"name":"rect","x":503,"y":88,"width":124,"height":165}';
'{"name":"rect","x":123,"y":78,"width":93,"height":111}' ;
'{"name":"rect","x":386,"y":47,"width":105,"height":112}';
'{"name":"rect","x":582,"y":40,"width":100,"height":113}';
'{"name":"rect","x":169,"y":50,"width":187,"height":209}';
'{"name":"rect","x":563,"y":49,"width":131,"height":181}';
'{"name":"rect","x":414,"y":128,"width":47,"height":53}';
'{"name":"rect","x":315,"y":131,"width":26,"height":33}'};
>> D = regexp(C,'\d+','match');
>> M = str2double(vertcat(D{:}))
M =
101 30 239 244
503 88 124 165
123 78 93 111
386 47 105 112
582 40 100 113
169 50 187 209
563 49 131 181
414 128 47 53
315 131 26 33
  1 Comment
Yuzhen Lu
Yuzhen Lu on 25 Jan 2020
What a magic function regexp! It is really what I need. Thanks!!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!