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 6095049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:46:09+00:00 2026-05-23T12:46:09+00:00

I have a file named data.dat with contents (sample): 0.0 2.3 4.5 0.9 0.5

  • 0

I have a file named data.dat with contents (sample):

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F

2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6

3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F

2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6

3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

.
.
.

I’m trying to merge every 3 rows array into one row array at the end to give a matrix of f x 29:

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

.
.
.

and then shift the 10th and 11th column to the 1st and 2nd row:

E F 0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

E F 0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

How do I do this in MATLAB? Here is my attempt, but it is incorrect.

% Find out number of rows in file

rline=0;
x=0;

% Open Data File
fid = fopen('data.dat','rt');

% Loop through data file until we get a -1 indicating EOF
while(x~=(-1))
  x=fgetl(fid);
  rline=rline+1;
end
rline = rline-1;

% How many row in final file

fline=rline/3; % one row in final file represent by 3 rows from raw data

% Create 3 seperate matrix named as z1,z2,z3

frewind(fid);
for i = 1:rline
  num1 = fscanf(fid,'%f %f %f %f %f %f %f %f %f\n')'; % Read in numbers
  name1 = fscanf(fid,'%s %s',rline); % Filter out string at end of line
  if(i==1)
    result1 = num1; % Add 1st row
    names1 = name1; % Add 1st text string
  else
    result1 = [result1;num1]; % Add additional rows
    names1 = char(names1,name1); % Add next string
    names1 = names1';
  end
  i=i+3;
end

fclose(fid);

z1 = result1;
zname= names1;

frewind(fid);
for i = 2:rline
  num2 = fscanf(fid,'%f')'; % Read in numbers

  if(i==2)
    result2 = num2; % Add 2nd row
  else
    result2 = [result2;num2]; % Add additional rows    
  end
  i=i+3;
end

fclose(fid);

z2 = result2;

frewind(fid);
for i = 3:rline
  num3 = fscanf(fid,'%f')'; % Read in numbers    
  if(i==3)
    result3 = num3; % Add 3rd row    
  else
    result3 = [result3;num3]; % Add additional rows

  end
  i=i+3;
end

fclose(fid);

z3 = result3;

% Create a final data matrix of F = (fline x 29)

for i = 1: fline
    for j = 1: fline
        F(i, [1:2]) = zname(j,:);
        F(i, [3:11]) = z1(j,:);
        F(i, [12:20]) = z2(j,:);
        F(i, [21:29]) = z3(j,:);
        j=j+1;
    end
    i=i+1;
end

Final_data = [F];
  • 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-23T12:46:10+00:00Added an answer on May 23, 2026 at 12:46 pm

    I am using the sample data file you’ve given as an example. The TEXTSCAN function is used to parse the file

    data.dat

    0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F
    2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6
    3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1
    0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F
    2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6
    3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1
    

    MATLAB code

    %# parse data file
    fid = fopen('data.dat','rt');
    C = textscan(fid, [repmat('%f ',[1 9]) '%s %s'], 'CollectOutput',true);
    fclose(fid);
    
    %# extract and reshape numeric data
    M = C{1};
    M = reshape(M', size(M,2)*3, [])';   %# similar to 'Michael J. Barber' answer
    
    %# extract textual data
    T = C{2}(1:3:end,:);
    
    %# we can merge all into one cell array
    data = [T num2cell(M)];
    

    Note that since the data contains heterogeneous types (numeric and characters), we read and store them separately. The final line of code shows one way of merging all data into a single cell array:

    data = 
        'E'    'F'    [0]    [2.3000]    [4.5000]    [0.9000]    [0.5000]    [3.4000]    [0]    [0.3000]    [0.5000]    [2.9000]    [5.4000]    [7.2000]    [4.8000]    [3.7000]    [9.1000]    [2.3000]    [4.1000]    [5.6000]    [3.4000]    [6.1000]    [4.8000]    [6.4000]    [0.4000]    [0.6000]    [0.3000]    [5.4000]    [7.1000]
        'E'    'F'    [0]    [2.3000]    [4.5000]    [0.9000]    [0.5000]    [3.4000]    [0]    [0.3000]    [0.5000]    [2.9000]    [5.4000]    [7.2000]    [4.8000]    [3.7000]    [9.1000]    [2.3000]    [4.1000]    [5.6000]    [3.4000]    [6.1000]    [4.8000]    [6.4000]    [0.4000]    [0.6000]    [0.3000]    [5.4000]    [7.1000]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data file named somedata.dat, it contains a list of number pairs.
i have one file (name.dat) which is binary data, it has 12 lines of
I have a file,named f1.txt, whose contents are 75 15 85 35 60 50
I have a file named file with three lines: line one line two line
I'm trying to save the contents of an object into a file, and then
I have 2 files Details.java and TestDetails.java and a data file called Details.dat Details.java
EDITED QUESTION: I have 2500 rows x 100 columns data in variable named avg_data_models.
I have a file named config.json. Here's its contents: { dataSources: [ http://www.blahblah.com/blah, http://www.blahblah.com/blah2,
I have this HTML: <form action='uploadhandle.php' method='POST' enctype=multipart/form-data> <input type='file' class='fileinput' id='photo1' name='photo1'> <input
I have an upload box... <form action=upload_file.php method=post enctype=multipart/form-data><BR> <label for=file>Filename:</label><BR> <input type=file name=file

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.