Remove a specific string from a cell matrix

조회 수: 4 (최근 30일)
Ivan Mich
Ivan Mich 2019년 10월 3일
편집: Adam Danz 2019년 10월 3일
Hello,
I have a question. I have a cell matrix 200x1 with the following format.
Temperature 10
Temperature 10
Temperature 25
Temperature 30
........................
Temperature 150.
I would like to use a command which will "remove" the string "Temperature".
Does anybody knows which command should I use to make it?

채택된 답변

Adam Danz
Adam Danz 2019년 10월 3일
편집: Adam Danz 2019년 10월 3일
Option 1: strrep() to replace 'Temperature ' with empty
C = {'Temperature 10';'Temperature 10';'Temperature 25';'Temperature 30'};
Ctrim = strrep(C,'Temperature ','') %cell array of chars containing only the numbers
Cnum = str2double(Ctrim); %vector of the numbers of class double.
Option 2: regexp() to extract the numeric characters
Ctrim = regexp(C,'\d+','match','once');
Cnum = ... %see above
Option 1 is faster.
  댓글 수: 2
Ivan Mich
Ivan Mich 2019년 10월 3일
Adam Danz Thank you very much !!!
Adam Danz
Adam Danz 2019년 10월 3일
Glad I could help!
BTW, the first solution, using strrep, is ~3.6 x faster (comparing median speeds using tic/toc across 100k iterations, each).

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by