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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:30:23+00:00 2026-06-19T01:30:23+00:00

I am writing several lines to a text file, among which is a matrix.

  • 0

I am writing several lines to a text file, among which is a matrix. I decided to use fprintf for the normal text messages and to use dlmwrite for writing the matrix to the file. However, this action is done in a while loop. Here is the outline:

 k=0;

 while (k<10):

     fprintf(file,'%s', 'Hello');

     dlmwrite(file,M ,'-append', 'newline', 'pc');

     fprintf(file, '%s' , 'Goodbye');

     k= K+1;

however, when I open the file, all the matrices are appended to the very end of the text file instead of each being between a hello and a goodbye. Is there a way to fix this issue?

  • 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-19T01:30:24+00:00Added an answer on June 19, 2026 at 1:30 am

    It may have something to do with the -append option, that according to the help appends the result to the end of the file.

    You are accessing the same file with two functions, fprintf and dlmwrite.

    This is probably not efficient, but closing the file after every write from fprintf would work:

    clear all
    close all
    
    file_name = 'aa.txt';
    file = fopen(file_name, 'w');
    fclose(file);
    file = fopen(file_name, 'a');
    M = randn(5);
    
    for kk = 1:10
        file = fopen(file_name, 'a');
        fprintf(file, 'Hi\n');
        fclose(file);
        dlmwrite(file_name, M ,'-append', 'newline', 'pc');
        file = fopen(file_name, 'a');
        fprintf(file, 'Bye\n');
    end
    

    If not, just try to print the matrix with other funcion that you create and that uses the same file handler as fprintf.

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

Sidebar

Related Questions

I'm currently writing several Python modules which perform some I/O. Thoses modules can be
Im writing a policy plugin for VS which checks several issues with the code.
I'm writing a class which shares several different features with std::function (or at least
I am writing an Android application which uses several 3D models. Such a model
I'm writing a page with several text fields and a drop down. The fields
I am writing a python module which checks several media and document formats whether
I have a CSS file which contains several hundred 16x16 icons. They are referenced
I am writing a tool take performs a task on text file. The task
I'm writing a script (show_volume.sh) which may be called several times in short intervals.
I have a file with several lines, each containing some 30,000 integers. I'm interested

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.