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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:56:00+00:00 2026-05-25T16:56:00+00:00

Say I want to print to disk the output of the command magic(20) using

  • 0

Say I want to print to disk the output of the command magic(20) using the automatic formatting capabilities in MATLAB (i.e. those of display and disp)

I would like to do this programatically from MATLAB. So my take so far has been:

First I configure my formatting options.

format bank
format compact

Then I open a file in text mode and write permission:

fID = fopen('output_file.txt', 'wt');

And then, I try to save the output of specific statements to disk:

1) With num2str

string = num2str(magic(20));
fwritef(fID, '%s', string);

2) With eval (based on the most-voted answer on this thread)

string = eval('magic(20)');
fwritef(fID, '%s', string);

Is there any way to use display or disp in combination with fprintf (or a similar text-file-writing API) to write disp/display-formatted strings to disk?

  • 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-25T16:56:01+00:00Added an answer on May 25, 2026 at 4:56 pm

    If you’re on linux or OS X you could run your script from the command line and redirect stdout to a file. You may want to check on the syntax, but it’s something like

    matlab -r my_function > out.txt
    

    I think there’s a way to do it from a DOSish prompt as well, though I don’t know the redirect syntax there.

    Update: Non-redirecting version

    Does something like this work?

    format bank
    format compact
    
    s1 = evalc('magic(4)');
    s2 = evalc('disp(magic(4))');
    
    f = fopen('test.txt', 'w');
    fprintf(f, '%s', s1);
    fprintf(f, '\n======================================\n\n');
    fprintf(f, '%s', s2);
    fclose(f)
    

    If I run this and then do !cat test.txt, I get

    ans =
         16.00          2.00          3.00         13.00
          5.00         11.00         10.00          8.00
          9.00          7.00          6.00         12.00
          4.00         14.00         15.00          1.00
    
    ======================================
    
         16.00          2.00          3.00         13.00
          5.00         11.00         10.00          8.00
          9.00          7.00          6.00         12.00
          4.00         14.00         15.00          1.00
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the code: print(>>> ) command = gets What I want to
Let's say you want to print out one of those great ASCII art images.
Say if i want to print out my integer stored inside a array ,
Consider: print $foo, AAAAAAAA, $foo, BBBBBBBB; Let's say I want to use this code
I have arraylist say, List<CalendarOutput> RecuringEve= Recurrent.eventView(component,begin,end); and i want to print the arraylist,
Lets say I want a mouse click event listener to trace (console print) the
Often in Perl I want to print out column/row data, say, from a hash.
Say I want to print html from inside a script tag. A source like
Let's say I want to print the first 3 items of a list. So
Say I want to print all my installed apps their version info on server

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.