Difference between revisions of "MATLAB:Diary"

From PrattWiki
Jump to navigation Jump to search
 
m
Line 2: Line 2:
  
 
== Using <code>echo</code> ==
 
== Using <code>echo</code> ==
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 <code>echo</code> 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 cho off (default) and the echo on:
+
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 <code>echo</code> 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 ===
 
=== EchoDemo.m ===

Revision as of 06:35, 21 October 2014


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            
!rm 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
!rm 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.