Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6761551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:12:24+00:00 2026-05-26T14:12:24+00:00

I have .txt files that contain the output from a data logger. The data

  • 0

I have .txt files that contain the output from a data logger. The data is logged in a very specific way, with one message per line, and with a message ID in front of each message. So, by knowing the message ID of each line, I will know what will follow on that line due to the message format. Each message (different ID) has a different format. An example of the data:

$GPGGA,220542.000,4745.8026,N,12211.0284,W,1,07,1.3,3.4,M,-17.2,M,,0000*67
$GPGSA,A,3,30,05,29,31,02,10,25,,,,,,2.0,1.3,1.5*3D
$GPGSV,3,1,12,29,78,315,39,05,52,080,45,30,43,288,46,25,41,196,33*72
$GPGSV,3,2,12,21,31,249,30,02,23,066,41,12,17,172,38,31,11,276,40*7F
$GPGSV,3,3,12,10,07,036,30,26,02,115,,18,01,199,,48,34,194,37*73
$GPRMC,220542.000,A,4745.8026,N,12211.0284,W,0.08,174.78,271011,,*12
$GPGGA,220543.000,4745.8025,N,12211.0284,W,1,07,1.3,3.4,M,-17.2,M,,0000*65

Everything is separated by commas, but since each line is different (ie each message does not have the same format), I can’t do anything with csv (in Matlab).

Basically, what I want to do is search through the data, line by line. For each line, I want to determine the message ID, then store the remainder of the line in an array, since I will know the format of the line. In the end, I will have an array for each message type.

I can put everything into excel using csv, but since each line is different it is hard to extract the data…and I don’t even know if that is possible in excel (it probably is).

In Matlab, I can’t use anything csv because there are non-numerical values. I have tried reading the file directly and grabbing each line using fgetl(), then stepping through each line, but there has to be a more efficient way. I read something about saving an excel file, then going to matlab to extract the data, but it would be nice to eliminate that intermediate step.

Ideas about how to go about this would be nice. I’m not looking for anyone to write code for me…just to point me in the write direction.

Oh, and I thought importdata() would work, but I tried importdata(‘filename.txt’,’,’), but it would not recognize the delimiter…?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:12:24+00:00Added an answer on May 26, 2026 at 2:12 pm

    Since your data has mixed formats, at some point in the process, you will have to loop over the lines and parse them according to your their respective formats. I doubt you will be able to parse the whole thing in one easy function call…

    Here is my attempt at reading the file as you gave it (you didn’t describe the format of the different log messages, so I had to came up with my own based on the few lines shown).

    The idea is to have for each message type, the format of the corresponding lines in the log file. We start by reading all lines from the file, and extract the message ID from each. Next for each possible message ID, we extract the matching lines, parse them one-by-one using the specified format and store the extracted info.

    The result will be a cell array, with one cell for each message ID. Each of those elements will be a cellarray on its own, storing as a table the rows/columns read.

    %# line format for each type of message IDs
    frmt = {
        '$GPGGA', '%s %f %f %c %f %c %f %f %f %f %c %f %c %f %s' ; 
        '$GPGSA', '%s %c %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %s' ; 
        '$GPGSV', '%s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %s' ; 
        '$GPRMC', '%s %f %c %f %c %f %c %f %f %f %f %s' ; 
    };
    
    %# read log file as lines
    fid = fopen('log.txt','rt');
    C = textscan(fid, '%s', Inf, 'Delimiter','\n'); C = C{1};
    fclose(fid);
    
    %# get message ID of each line
    msgId = strtok(C,',');
    
    %# for each possible message ID
    arr = cell(size(frmt,1),1);
    for m=1:size(frmt,1)
        %# get lines matching this ID
        lines = C( ismember(msgId,frmt{m,1}) );
    
        %# parse lines using specified format
        arr{m} = cell(numel(lines), sum(frmt{m,2}=='%'));
        for i=1:numel(lines)
            arr{m}(i,:) = textscan(lines{i}, frmt{m,2}, 'Delimiter',',');
        end
    
        %# flatten nested cells containing strings
        idx = cellfun(@iscell, arr{m}(1,:));
        arr{m}(:,idx) = cellfun(@(x)x, arr{m}(:,idx));
    end
    

    log.txt

    $GPGGA,220542.000,4745.8026,N,12211.0284,W,1,07,1.3,3.4,M,-17.2,M,,0000*67
    $GPGSA,A,3,30,05,29,31,02,10,25,,,,,,2.0,1.3,1.5*3D
    $GPGSV,3,1,12,29,78,315,39,05,52,080,45,30,43,288,46,25,41,196,33*72
    $GPGSV,3,2,12,21,31,249,30,02,23,066,41,12,17,172,38,31,11,276,40*7F
    $GPGSV,3,3,12,10,07,036,30,26,02,115,,18,01,199,,48,34,194,37*73
    $GPRMC,220542.000,A,4745.8026,N,12211.0284,W,0.08,174.78,271011,,*12
    $GPGGA,220543.000,4745.8025,N,12211.0284,W,1,07,1.3,3.4,M,-17.2,M,,0000*65
    

    The result for the above file:

    >> arr
    arr = 
        {2x15 cell}
        {1x18 cell}
        {3x20 cell}
        {1x12 cell}
    

    for example, the rows matching messageID = $GPGGA are:

    >> arr{ ismember(frmt(:,1),'$GPGGA') }     %# arr{1}
    ans = 
        '$GPGGA'    [220542]    [4745.8]    'N'    [12211]    'W'    [1]    [7]    [1.3]    [3.4]    'M'    [-17.2]    'M'    [NaN]    '0000*67'
        '$GPGGA'    [220543]    [4745.8]    'N'    [12211]    'W'    [1]    [7]    [1.3]    [3.4]    'M'    [-17.2]    'M'    [NaN]    '0000*65'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 files: one (ranges.txt) with rows that contain 2 tab-delimited integers, and
I have a jar file that uses some txt files. In order to get
I have names of all files in output.txt but I do not have folderpath
I have a file that can contain from 3 to 4 columns of numerical
I have a blacklist.txt file that contains keywords I want to remove using sed.
I have a .txt file that I need to include into a page, that
For example, I have a txt file that reads: 12 345 45 2342 234
I have a CMakeLists.txt file that looks like this: add_executable(exec1 exec1.c source1.c source2.c source3.c)
What I have is a txt file that is huge, 60MB. I need to
I have a txt file with columns separated by tabs and based on that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.