deleting part of a list of strings

조회 수: 9 (최근 30일)
mb1400
mb1400 2013년 6월 20일
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

답변 (7개)

David Sanchez
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.

Thorsten
Thorsten 2013년 6월 20일
newstr = str(:, 1:findstr(str(1,:), 'E'))
  댓글 수: 1
Thorsten
Thorsten 2013년 6월 20일
If the position of the 'E' may be different for each line, use
for i=1:size(str, 1)
newstr(i,:) = str(:, 1:findstr(str(i,:), 'E'));
end

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


mb1400
mb1400 2013년 6월 20일
E_position = find(out2=='E'); ??? Undefined function or method 'eq' for input arguments of type 'cell'.
newstr = out2(:, 1:findstr(out2(1,:), 'E')) ??? Error using ==> findstr Inputs must be character arrays.
not working in both ways
arrrrrghhhhhh.
And also. By the way. I do I extract the final result into a text file?

mb1400
mb1400 2013년 6월 20일
The second way it did not really cut after E, that's what it returned
newstr = out3(:, 1:findstr(out3(1,:), 'E'))
newstr =
$SDDBT,00004.9,f,0001.5,M,0000.8,F*07,$GPGLL,5340.91664087,N,00713.79587546,E $SDDBT,00004.5,f,0001.4,M,0000.7,F*05004.5,f,0001.4,M,0000.7,F*02,$GPGLL,5340 $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
  댓글 수: 1
Thorsten
Thorsten 2013년 6월 20일
편집: Thorsten 2013년 6월 20일
Please see my revision above using a for loop to determine the correct position of 'E' for each line. (In your sample data in the question the 'E' appeared always at the same position.)

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


David Sanchez
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);

Matt Tearle
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')'

mb1400
mb1400 2013년 6월 20일
I will soon try Matt's advice and get back to you. To Thorsten the problem is that the string is created from a combination of 2 strings. The original file looked something like this.
$SDDBT,00004.9,f,0001.5,M,0000.8,F*07 $SDDBS,00004.9,f,0001.5,M,0000.8,F*00 $VWVHW,,T,,M,00.0,N,,K*4A $SDRMB,A,13.74,L,00,TEST1,5330.546,N,00807.276,E,033.4,108,,*45 $GPGLL,5340.91664087,N,00713.79587546,E,073418.00,A,D*62 $GPHDT,9.421,T*3B $GPVTG,193.65,T,,M,5.82,N,10.77,K,D*0E $GPVTG,192.06,T,,M,5.54,N,10.26,K,D*05 $GPHDT,9.428,T*32 $GPGLL,5340.91360433,N,00713.79468132,E,073420.00,A,D*64 $GPHDT,9.430,T*3B .35,K,D*0F $GPHDT,9.431,T*3A $SDDPT,0001.4,000.0*62 $SDDBT,00004.5,f,0001.4,M,0000.7,F*05004.5,f,0001.4,M,0000.7,F*02 $VWVHW,,T,,M,00.0,N,,K*4A $SDRMB,A,13.74,L,00,TEST1,5330.546,N,00807.276,E,033.4,108,,*45 $GPGLL,5340.91207731,N,00713.79413391,E,073421.00,A,D*63 $GPHDT,9.433,T*38 $GPVTG,193.32,T,,M,5.69,N,10.54,K,D*08 $GPHDT,9.435,T*3E $GPGLL,5340.91056222,N,00713.79354123,E,073422.00,A,D*6E $SDDPT,0002.0,000.0*65 $SDDBT,00006.5,f,0002.0,M,0001.0,F*06 $SDDBS,00006.5,f,0002.0,M,0001.0,F*01 $VWVHW,,T,,M,00.0,N,,K*4A $SDRMB,A,13.75,L,00,TEST1,5330.546,N,00807.276,E,033.4,108,,*44 $GPHDT,9.437,T*3C
I was helped to write a script that with a loop would extract the SDDPT and GPGLL strings, it was a complicated process since strings have not always the same occurence, all I wanted was the first SDDPT and the successive associated GPGLL, all in one line. So, this is what we did.
for j=1:length(data);
if strncmp('$SDDBT',data(j,1), 6)==1;
% nuova_variabile=[nuova_variabile; vertcat(data{1,1}{j,1})];
nuova_variabile=[nuova_variabile; (data(j,1))];
elseif strncmp('$GPGLL',data(j,1), 6)==1; %%stanno sempre sotto?
nuova_variabile=[nuova_variabile; (data(j,1))];
end
end
end
b=char(nuova_variabile)
c=cellstr(b(:,1:6)) idx=strcmp(c,'$SDDBT')'; ii=[1 diff(idx)]; out=nuova_variabile(find(ii~=0))
ne=ceil(numel(out)/2) out=cellfun(@(x,y) [x ' ' y],out(1:2:end), out(2:2:end),'un',0); expression = ' '; replace = ','; out2= regexprep(out,expression,replace); and this is what we ended up with:
'$SDDBT,00004.9,f,0001.5,M,0000.8,F*07,$GPGLL,5340.91664087,N,00713.79587546,E,073418.00,A,D*62' [1x122 char] '$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'
The problem is that the SDDBT string is not always of the same lenght. Cutting at E for the whole cell, would not be appropriate. I am completely new in matlab and I am still learning. Working with string is not that easy for me. I would immagine that I could have split the two type of string into two colums, trim and then ricombine.
MATLAB is driving me MAD!!!!!!!!!!!!!!!!!!!!!

카테고리

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