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 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

Related Questions

I have a full MS SQL Backup file that I would like to extract
I'm probably doing this all wrong. I have a text file full of data
I have a big file full of integers that I'm loading in. I've just
I have a really crappy file full of unicode bytes that I'm trying to
I have a data file full of numbers I'm loading into a vector of
I have the following file full of lines similar to this: line = 'Weclome
I have a text file full of records (output.txt) and I want to sort
I have a text file full of stopwords. I would like to represent this
I have a *.TXT file full of about 1 - 200 lines. Each line
I hava text file full of values like this: The first line is a

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.