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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:39:56+00:00 2026-05-12T19:39:56+00:00

I have a file full of ascii data. How would I append a string

  • 0

I have a file full of ascii data. How would I append a string to the first line of the file? I cannot find that sort of functionality using fopen (it seems to only append at the end and nothing else.)

  • 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-12T19:39:57+00:00Added an answer on May 12, 2026 at 7:39 pm

    Option 1:

    I would suggest calling some system commands from within MATLAB. One possibility on Windows is to write your new line of text to its own file and then use the DOS for command to concatenate the two files. Here’s what the call would look like in MATLAB:

    !for %f in ("file1.txt", "file2.txt") do type "%f" >> "new.txt"
    

    I used the ! (bang) operator to invoke the command from within MATLAB. The command above sequentially pipes the contents of “file1.txt” and “file2.txt” to the file “new.txt”. Keep in mind that you will probably have to end the first file with a new line character to get things to append correctly.

    Another alternative to the above command would be:

    !for %f in ("file2.txt") do type "%f" >> "file1.txt"
    

    which appends the contents of “file2.txt” to “file1.txt”, resulting in “file1.txt” containing the concatenated text instead of creating a new file.

    If you have your file names in strings, you can create the command as a string and use the SYSTEM command instead of the ! operator. For example:

    a = 'file1.txt';
    b = 'file2.txt';
    system(['for %f in ("' b '") do type "%f" >> "' a '"']);
    

    Option 2:

    One MATLAB only solution, in addition to Amro’s, is:

    dlmwrite('file.txt',['first line' 13 10 fileread('file.txt')],'delimiter','');
    

    This uses FILEREAD to read the text file contents into a string, concatenates the new line you want to add (along with the ASCII codes for a carriage return and a line feed/new line), then overwrites the original file using DLMWRITE.

    I get the feeling Option #1 might perform faster than this pure MATLAB solution for huge text files, but I don’t know that for sure. 😉

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

Sidebar

Ask A Question

Stats

  • Questions 285k
  • Answers 285k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer there is not [yet] a version of numpy that has… May 13, 2026 at 4:44 pm
  • Editorial Team
    Editorial Team added an answer You have a couple of options: & operator: ="blah blah… May 13, 2026 at 4:44 pm
  • Editorial Team
    Editorial Team added an answer You can set up your emacs-server to use a tcp… May 13, 2026 at 4:44 pm

Related Questions

I've been trying to deal with some delimited text files that have non standard
I have a bunch of PDF files and my Perl program needs to do
I have part of a build process that creates a hideously long paths in
I'm having a hard time finding a good resource that explains how to use

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.