How to remove first charachter from a string inside a cell

조회 수: 9 (최근 30일)
Ramiro Rea
Ramiro Rea 2017년 7월 25일
댓글: Ramiro Rea 2017년 7월 25일
Hi everyone,
I have a 6 x 5 cell array (see example file). I would like, for the 3th and 4th column to remove the first character (the symbol $), so I can do some operations with the data. I know how to do remove this in a single string, but when I am working with the column, it doesn't work. For instance if I put:
a = data_SVO{6,3};
c = c = a(2:end);
c =
6.3
Then c is the value without the '$'. I know probably is really simple, but I haven't managed to find a solution to this. I would appreciate if you could guide me to solve this issue.
Many thanks

채택된 답변

per isakson
per isakson 2017년 7월 25일
편집: per isakson 2017년 7월 25일
One way with R2016a
>> load 20_SVO.MAT
>> data_SVO(:,3:4) = regexprep( data_SVO(:,3:4), '\$', '' );
>> data_SVO
data_SVO =
[6] [9] '8.5' '8.5' [ 5.9365]
[3] [9] '8.5' '8.5' [ 3.9902]
[5] [5] '7.5' '7.5' [14.4665]
[1] [1] '8.5' '8.5' [ 6.0439]
[2] [9] '10' '5' [20.5387]
[4] [4] '6.3' '6.8' [15.7036]
>>
or use a double loop.
Variants
  • Replace '\$' by '^\$' to be sure to only remove "$" when it's in the first position.
  • Replace '\$' by '^.' to remove the first character whatever it is.
  댓글 수: 1
Ramiro Rea
Ramiro Rea 2017년 7월 25일
Thanks, this solves the issue. By any chance do you know how to transform this columns to numbers? cell2mat doesn't work. If I transpose the column to use cell2mat,it does this:
d = data_SVO(:,3);
c = cell2mat(d.');
c =
8.58.57.58.5106.3
I know is not related to the original question, but maybe you have a solution for this. Thanks again.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by