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?
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
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?
If I run this and then do
!cat test.txt, I get