How to extract numeric values from char

조회 수: 50 (최근 30일)
Yuzhen Lu
Yuzhen Lu 2020년 1월 25일
댓글: Yuzhen Lu 2020년 1월 25일
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?

채택된 답변

Stephen23
Stephen23 2020년 1월 25일
>> 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
Yuzhen Lu
Yuzhen Lu 2020년 1월 25일
What a magic function regexp! It is really what I need. Thanks!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by