deleting part of a list of strings
조회 수: 9 (최근 30일)
이전 댓글 표시
I have a column of strings, with the same number of element in each string, all comma delimited. I would like to trim all the strings after the last coordinate to have something from this:
'$SDDBT,00006.5,f,0002.0,M,0001.0,F*06,$GPGLL,5340.90756479,N,00713.79236750,E,073424.00,A,D*61' '$SDDBT,00006.8,f,0002.1,M,0001.1,F*0B,$GPGLL,5340.90609055,N,00713.79168827,E,073425.00,A,D*66' '$SDDBT,00007.8,f,0002.4,M,0001.3,F*0D,$GPGLL,5340.90462662,N,00713.79106230,E,073426.00,A,D*6C' To this:
'$SDDBT,00006.5,f,0002.0,M,0001.0,F*06,$GPGLL,5340.90756479,N,00713.79236750,E' '$SDDBT,00006.8,f,0002.1,M,0001.1,F*0B,$GPGLL,5340.90609055,N,00713.79168827,E' '$SDDBT,00007.8,f,0002.4,M,0001.3,F*0D,$GPGLL,5340.90462662,N,00713.79106230,E' I appreciate the help
댓글 수: 0
답변 (7개)
David Sanchez
2013년 6월 20일
Since in you GPS data you want to trim right after the "E", you can try this:
str='$SDDBT,00006.5,f,0002.0,M,0001.0,F*06,$GPGLL,5340.90756479,N,00713.79236750,E,073424.00,A,D*61' ;
E_position = find(str=='E');
new_str = srt(1:E_position);
do it for each string.
댓글 수: 0
David Sanchez
2013년 6월 20일
do you have a cell array containing your strings?
If my_cell is your cell array, try:
str = char(my_cell{k});
for the k-th cell. Then:
E_position = find(str=='E');
new_str = srt(1:E_position);
Or:
E_position = findstr(str,'E')
new_str = srt(1:E_position);
댓글 수: 0
Matt Tearle
2013년 6월 20일
편집: Matt Tearle
2013년 6월 20일
Assuming you have an n-by-1 cell array of strings to start with (and you'd like an n-by-1 cell arrays of strings at the end), here are a couple of ways you can do it:
oldstr = {'$SDDBT,00006.5,f,0002.0,M,0001.0,F*06,$GPGLL,5340.90756479,N,00713.79236750,E,073424.00,A,D*61';'$SDDBT,00006.8,f,0002.1,M,0001.1,F*0B,$GPGLL,5340.90609055,N,00713.79168827,E,073425.00,A,D*66';'$SDDBT,00007.8,f,0002.4,M,0001.3,F*0D,$GPGLL,5340.90462662,N,00713.79106230,E,073426.00,A,D*6C'};
bits = regexp(oldstr,',','split');
f = @(x) strjoin(x(1:12),',');
newstr = cellfun(f,bits,'uniform',false)
This uses strjoin which is a fairly recent function. Alternatively,
oldstr = {'$SDDBT,00006.5,f,0002.0,M,0001.0,F*06,$GPGLL,5340.90756479,N,00713.79236750,E,073424.00,A,D*61';'$SDDBT,00006.8,f,0002.1,M,0001.1,F*0B,$GPGLL,5340.90609055,N,00713.79168827,E,073425.00,A,D*66';'$SDDBT,00007.8,f,0002.4,M,0001.3,F*0D,$GPGLL,5340.90462662,N,00713.79106230,E,073426.00,A,D*6C'};
bits = textscan(sprintf('%s\n',oldstr{:}),...
[repmat('%s',1,12),repmat('%*s',1,3)],...
'delimiter',',','collectoutput',true);
tmp = bits{1}';
newstr = regexp(deblank(sprintf([repmat('%s,',1,11),'%s\n'],tmp{:})),'\n','split')'
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!