Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Extraction of portion of textfile

Asked by mb1400

mb1400 (view profile)

on 26 Jun 2013

I have text string stored in a variable. The text file is just an example but, the .txt(s) I will work on may not always start with the same string code. I want to delete all the strings above the first $GPGLL, if this is not the first, and I want to delete all the strings below the last $SDDBT string and store in a new variable, below is my original variable, the number of strings is not always the same. Thumbs up for the good samaritan that will help me in this task.

new_variable= $GPHDT,9.420,T*3A $GPGLL,5340.91664087,N,00713.79587546,E,073418.00,A,D*62 $GPVTG,193.15,T,,M,5.72,N,10.59,K,D*0A $GPHDT,9.420,T*3A $SDDPT,0001.5,000.0*63 $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 $GPVTG,191.91,T,,M,5.51,N,10.20,K,D*0B $GPHDT,9.439,T*32 *3D $GPVTG,194.87,T,,M,5.32,N,9.86,K,D*38 $GPHDT,9.443,T*3F $GPGLL,5340.90756479,N,00713.79236750,E,073424.00,A,D*61 $GPHDT,9.446,T*3A $SDDPT,0002.1,000.0*64 $SDDBT,00006.8,f,0002.1,M,0001.1,F*0B $SDDBS,00006.8,f,0002.1,M,0001.1,F*0C $VWVHW,,T,,M,00.0,N,,K*4A $SDRMB,A,13.75,L,00,TEST1,5330..4,108,,*44 $GPVTG,194.65,T,,M,5.49,N,10.17,K,D*08 $GPHDT,9.449,T*35 $GPGLL,5340.90609055,N,00713.79168827,E,073425.00,A,D*66 $GPHDT,9.452,T*3F $GPVTG,193.15,T,,M,5.52,N,10.22,K,D*04 $GPHDT,9.454,T*39 $SDDPT,0002.4,000.0*61 $SDDBT,00007.8,f,0002.4,M,0001.3,F*0D $SDDBS,00007.8,f,0002.4,M,0001.3,F*0A $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 $GPGLL,5340.90462662,N,00713.79106230,E,073426.00,A,D*6C

0 Comments

mb1400

mb1400 (view profile)

Products

No products are associated with this question.

1 Answer

Answer by Andrei Bobrov

Andrei Bobrov (view profile)

on 26 Jun 2013
Edited by Andrei Bobrov

Andrei Bobrov (view profile)

on 26 Jun 2013
f = fopen('nameYourTextFile.txt');
c = textscan(f,'%s','delimiter','\n');
fclose(f);
out = c{:}(find(strncmp(c{:},'$GPGLL',6),1,'first')...
                                     :find(strncmp(c{:},'$SDDBT',6),1,'last'));   

ADD

new_variable= '$GPHDT,9.420,T*3A $GPGLL,5340.91664087,N,00713.79587546,E,073418.00,A,D*62 $GPVTG,193.15,T,,M,5.72,N,10.59,K,D*0A $GPHDT,9.420,T*3A $SDDPT,0001.5,000.0*63 $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 $GPVTG,191.91,T,,M,5.51,N,10.20,K,D*0B $GPHDT,9.439,T*32 *3D $GPVTG,194.87,T,,M,5.32,N,9.86,K,D*38 $GPHDT,9.443,T*3F $GPGLL,5340.90756479,N,00713.79236750,E,073424.00,A,D*61 $GPHDT,9.446,T*3A $SDDPT,0002.1,000.0*64 $SDDBT,00006.8,f,0002.1,M,0001.1,F*0B $SDDBS,00006.8,f,0002.1,M,0001.1,F*0C $VWVHW,,T,,M,00.0,N,,K*4A $SDRMB,A,13.75,L,00,TEST1,5330..4,108,,*44 $GPVTG,194.65,T,,M,5.49,N,10.17,K,D*08 $GPHDT,9.449,T*35 $GPGLL,5340.90609055,N,00713.79168827,E,073425.00,A,D*66 $GPHDT,9.452,T*3F $GPVTG,193.15,T,,M,5.52,N,10.22,K,D*04 $GPHDT,9.454,T*39 $SDDPT,0002.4,000.0*61 $SDDBT,00007.8,f,0002.4,M,0001.3,F*0D $SDDBS,00007.8,f,0002.4,M,0001.3,F*0A $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 $GPGLL,5340.90462662,N,00713.79106230,E,073426.00,A,D*6C';
ccc = mat2cell(new_variable,1,diff([regexp(new_variable,'\$'),...
                                            numel(new_variable)+1]))';
out = ccc(find(strncmp(ccc,'$GPGLL',6),1,'first'):...
                              find(strncmp(ccc,'$SDDBT',6),1,'last'));

2 Comments

mb1400

mb1400 (view profile)

on 26 Jun 2013

??? Error using ==> strncmp Too many input arguments.

that's what it returns

Andrei Bobrov

Andrei Bobrov (view profile)

on 26 Jun 2013

Please, see ADD part in my answer.

Andrei Bobrov

Andrei Bobrov (view profile)

Contact us