필터 지우기
필터 지우기

Editing a cell array.

조회 수: 3 (최근 30일)
Gagan Bansal
Gagan Bansal 2019년 1월 30일
편집: Stephen23 2019년 1월 30일
I have a cell array
1ms 1kA 10V
2ms 2.2kA 3V
3ms 3.6kA 5.4V
I want to remove 'ms', 'kA' & 'V' from the cell array and then convert it into a matlab matrix with numerical value for further calculations.
  댓글 수: 1
KSSV
KSSV 2019년 1월 30일
Read about regexp

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

답변 (2개)

madhan ravi
madhan ravi 2019년 1월 30일
편집: madhan ravi 2019년 1월 30일
C={'1ms' '1kA' '10V'
'2ms' '2.2kA' '3V'
'3ms' '3.6kA' '5.4V'};
Matrix=str2double(cellfun(@(x)regexp(x,'^(\d{0,5}\.{0,1}\d{0,5})','match'),C))
  댓글 수: 2
Stephen23
Stephen23 2019년 1월 30일
편집: Stephen23 2019년 1월 30일
Note that regexp accepts a cell array of character vectors, so the cellfun call is totally superfluous. I recommend using the option 'once' to simplify the output handling.
Some notes about the regular expression itself:
\d{0,5}
why limited to just five digits?
\.{0,1}
is simpler as
\.?
The grouping parentheses are not required.
madhan ravi
madhan ravi 2019년 1월 30일
편집: madhan ravi 2019년 1월 30일
Agree with Stephen:
Matrix=cellfun(@str2double,regexp(C,'^(\d*\.?\d*)','match'))
% or
Matrix=str2double(regexp(C,'^(\d*\.?\d*)','match','once'))
Gives:
Matrix =
1.0000 1.0000 10.0000
2.0000 2.2000 3.0000
3.0000 3.6000 5.4000

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


Stephen23
Stephen23 2019년 1월 30일
편집: Stephen23 2019년 1월 30일
To get the actual numeric values taking into account the SI prefixes you can use my FEX submission sip2num, which can be downloaded from FEX:
>> C = {'1ms','1kA','10V';'2ms','2.2kA','3V';'3ms','3.6kA','5.4V'}
C =
'1ms' '1kA' '10V'
'2ms' '2.2kA' '3V'
'3ms' '3.6kA' '5.4V'
>> N = cellfun(@sip2num,C)
N =
0.001 1000 10
0.002 2200 3
0.003 3600 5.4

카테고리

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