MATLAB:Diary

From PrattWiki
Jump to navigation Jump to search


Using echo

If, when creating your diary, you want to show not only the results that were displayed in the command window but also the commands that produced those results, you can use the echo command. This command in a script of function will tell MATLAB to first "echo" the line of code in the command window, then display any result of that line of code. Here is an example code that shows what a diary will look like with the echo off (default) and the echo on:

EchoDemo.m

clear

% turn/keep echo off
echo off
% remove old file if there            
delete('NoEcho.txt')    
% start diary named NoEcho.txt  
diary NoEcho.txt    
a = [1 3 5; 2 4 6]
b = a + a
cos(a)
% close diary
diary off 

% turn/keep echo on
echo on             
% remove old file if there
delete('WithEcho.txt')    
% start diary named WithEcho.txt
diary WithEcho.txt  
a = [1 3 5; 2 4 6]
b = a + a
cos(a)
% close diary
diary off

NoEcho.txt

a =
     1     3     5
     2     4     6
b =
     2     6    10
     4     8    12
ans =
   5.4030e-01  -9.8999e-01   2.8366e-01
  -4.1615e-01  -6.5364e-01   9.6017e-01


WithEcho.txt

a = [1 3 5; 2 4 6]
a =
     1     3     5
     2     4     6
b = a + a
b =
     2     6    10
     4     8    12
cos(a)
ans =
   5.4030e-01  -9.8999e-01   2.8366e-01
  -4.1615e-01  -6.5364e-01   9.6017e-01
% close diary
diary off

The second case would be useful in showing both which commands were run and their results, which the first is useful if you only need the results. In the specific case of cos(a), the first code does not have any indication of where the ans came from. Also, note that any comments in the code after echo is turned on and the diary is started also show up.

Printing Text Files with Linux

For MATLAB scripts or diaries, or any other text file, you can print out the scripts and diary files in UNIX with the command enscript. Specifically,

enscript FILENAME

will send the file directly to the printer. If you want to look at the file first, print to a file, convert it to a PDF, and then use evince to look at that file:

enscript FILENAME -pOUTPUT.ps
convert OUTPUT.ps OUTPUT.pdf
evince OUTPUT.pdf

This way, you will have a PDF file ready for uploading if you need one for the assignment or you can print it from evince.