deleting part of a list of strings
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (7)
David Sanchez
am 20 Jun. 2013
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 Kommentare
David Sanchez
am 20 Jun. 2013
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 Kommentare
Matt Tearle
am 20 Jun. 2013
Bearbeitet: Matt Tearle
am 20 Jun. 2013
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 Kommentare
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!