MATLAB Answers

How to extract numbers from a string?

조회 수: 39(최근 30일)
John Doe
John Doe 12 Jul 2020 20:18
댓글: John Doe 12 Jul 2020 20:51
xx = '[10,10]'
I want to convert the string xx to a number and extract the first and second value in a single line. What I am trying to do is extract the 1st and 2nd value without storing them in any variable.
Is there any solution to this? Thanks!

  댓글 수: 0

Sign in to comment.

채택된 답변

madhan ravi
madhan ravi 12 Jul 2020 20:21
편집: madhan ravi 12 Jul 2020 20:28
sscanf(xx, '[%d, %d]', [1,inf])
%or
str2double(regexp(xx, '\d*', 'match'))

  댓글 수: 3

John Doe
John Doe 12 Jul 2020 20:33
Thanks Madhan for the prompt reply.
Could you please tell me what to do if I want to extract the second value only from xx?
madhan ravi
madhan ravi 12 Jul 2020 20:49
Rather complicated:
subsref(str2double(regexp(xx,'\d*','match')), substruct('()', {2}))
John Doe
John Doe 12 Jul 2020 20:51
Thanks! My day is saved!

Sign in to comment.

추가 답변(0개)


Translated by