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

  • Home
  • SEARCH
  • 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 8843231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:10:09+00:00 2026-06-14T11:10:09+00:00

I have a text file containing message and timestamps: {193220,message,ucontroller9,controller1,lowalarm,success}, {193220,message,controller1,changer,{change,down},success}, {193220,controlaction,tapchange_down,{oldsetting,0.975},{newsetting,0.95}}, {193220,modelupdate_start,changeup,changer,193220}, {14430,modelupdate_complete},

  • 0

I have a text file containing message and timestamps:

 {193220,message,ucontroller9,controller1,lowalarm,success},
 {193220,message,controller1,changer,{change,down},success},
 {193220,controlaction,tapchange_down,{oldsetting,0.975},{newsetting,0.95}},
 {193220,modelupdate_start,changeup,changer,193220},
 {14430,modelupdate_complete},
 {278480,message,ucontroller6,controller1,highalarm,success},
 {278480,message,controller1,changer,{change,up},success},
 ...

I would like to read these lines into an array and re-order the array based on the first column. My approach so far has been as follows:

fid = fopen('messagestore.txt')    % Open file
n=1    
while 1
string = fgetl(fid);               % get line
if ~ischar(string), break, end     % break if end of file
string = strrep(string,'[','');    %
string = strrep(string,']','');    %
string = strrep(string,'{','');    % strip out unwanted characters
string = strrep(string,'}','');    %
string = strrep(string,',',' ');   %
string = string(2:end);            % remove space at start

MessageArray(:,n) = textscan(string,'%f%s%s%s%s%s');  % format and save into array
n=n+1;
end
fclose(fid) 

This partially works but I am having a problem with discriminating between different message types (i.e. number of fields). I’m guessing that I could use the matlab sort function if I could just get the data into an array properly.

Is there a way to do this in matlab?

  • 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-06-14T11:10:11+00:00Added an answer on June 14, 2026 at 11:10 am

    Dennis is right in saying that you probably should do some preprocessing, since you have multiple sets of braces and such.

    What you may be looking for is some way of basically sorting the lines of the file. If you’re not worried about capturing the individual string fields, but just in re-ordering lines, you can read and sort according to the first field, which you know to be numbers. This is pretty hackish, but maybe you’re looking for something like (this is essentially what Dennis suggests in the comments above):

    clear all, clc
    fid = fopen('messagestore.txt');
    n=1;
    nums = [];
    while 1
        string = fgetl(fid);               % get line
        if ~ischar(string), break, end     % break if end of file
    
        for k = 1:length(string)
            if string(k) == ','
                break
            end
        end
        nums = [nums str2num(string(2:k-1))];
        MessageArray{n} = string;
        n=n+1;
    end
    [nums,I] = sort(nums);
    MessageArray = MessageArray(I);
    fclose(fid);
    
    % Print some output
    for k = 1:length(nums)
        disp(MessageArray{k})
    end
    

    which produces

    {14430,modelupdate_complete},
    {193220,message,ucontroller9,controller1,lowalarm,success},
    {193220,message,controller1,changer,{change,down},success},
    {193220,controlaction,tapchange_down,{oldsetting,0.975},{newsetting,0.95}},
    {193220,modelupdate_start,changeup,changer,193220},
    {278480,message,ucontroller6,controller1,highalarm,success},
    {278480,message,controller1,changer,{change,up},success}
    

    Now, I am not entirely sure how your timestamps work, here I literally sort the numerical value. But you should be able to get the basic idea from the code — read the numbers from the first field into a vector and use that vector to sort the lines themselves. Whatever type of sorting your timestamps require can be performed by changing [nums,I] = sort(nums)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file containing strings to encrypt. These strings are indicated by
I have a text file containing 5 columns of data. The first column contains
I have a text file containing the data in following format 12345 Abdt3 hy45d
I have a text file containing unwanted null characters (ASCII NUL, \0 ). When
I have a text file containing a number of records. Each record is stored
I have a text file containing words separated by newline , like the following
I have a text file containing two or more types of lines. I would
I have a text file (txt) containing formatted text (just line breaks, carriage returns
I have to filter a text file filter.tmp containing two types of lines, this
In a text file I will have a line containing a series of numbers,

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.