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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:27:49+00:00 2026-05-27T09:27:49+00:00

Assume there’s a myfile.csv with variable names in the first row and decimal numbers

  • 0

Assume there’s a myfile.csv with variable names in the first row and decimal numbers in the following ones. In Matlab I’d like to read the header line and the decimal numbers separately. So far, I’ve been doing the following to extract the header line:

fid = fopen('myfile.csv');
a = textscan(fid,'%s','Delimiter','\n');
b = a{1,1};
fclose(fid);
c = textscan(b,'%s','Delimiter',',');
d = c{1}

Then, I use the csvread command to extract the numerical part of the file. But there should be a (much) easier way to do it! First, I don’t want to read the whole file (as with a = textscan(fid,'%s','Delimiter','\n');) to extract only the first line. Second, it looks wrong to use 7 lines of code to do it – can it be done with less?

I’d be thankful for any constructive suggestions.

  • 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-27T09:27:49+00:00Added an answer on May 27, 2026 at 9:27 am

    Open the file with fopen, read the header line with textscan, read the decimal numbers with fscanf, and call fclose in the end – only 4 lines in total 🙂 Example input file:

    Weight,Size,Count
    1,2,3
    4,5,6
    7,8,9
    10,11,12
    

    Reading of this file:

    fid = fopen('myfile.csv', 'r');
    header = textscan(fid, '%[^,],%[^,],%[^,\r\n]', 1);
    data = transpose(fscanf(fid, '%g,%g,%g\n', [3, Inf]));
    fclose(fid);
    
    for i = 1 : 3; disp(['"' cell2mat(header{i}) '"']); end;
    disp(data);
    

    Note that the data read by fscanf need to be transposed (I emphasized this by writing transpose instead of '). The output:

    "Weight"
    "Size"
    "Count"
         1     2     3
         4     5     6
         7     8     9
        10    11    12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume there exists an XML instance document that looks like this: <root> <object type=foo>
Assume there is a cell array initialized with the following struct values. % Phone
Let's assume there is a SQL Server 2008 table like below, that holds 10
Assume there is a class with the following interface: #import <Foundation/Foundation.h> @interface MyClass :
Assume there is a list of domain names but you dont know whether they
Let's assume there are 2 applications. The POM hierarchy might look like this: parent
I assume there must be a system and language independent way to just stick
How do I have a script run every, say 30 minutes? I assume there
Assume I have created a compiled re: x = re.compile('^\d+$') Is there a way
Is there any performance difference between the for loops on a primitive array? Assume:

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.