% MATLAB SCRIPT FILE s322eg1.m  */
%
%      NOTE THAT ANYTHING AFTER THE FIRST % IN A LINE IS "COMMENTED"
%      I. E. THERE WILL BE NO EXECUTION, SO THIS CAN BE USED FOR
%      PUTTING REMINDERS TO YOURSELF ABOUT WHAT THE PROGRAM IS DOING.
%      MANY SUCH ARE INCLUDED IN THIS PROGRAM.

%      IT ALSO CAN BE USED FOR "HEADERS" AS ABOVE.  THE MATLAB
%      COMMAND "HELP SCRIPTNAME" DISPLAYS ALL LINES STARTING WITH %,
%      UNTIL THE FIRST LINE NOT STARTING WITH %.  THIS IS USEFUL FOR
%      REMINDING YOURSELF WHAT IS IN YOUR SCRIPT FILES.  SEE WHAT
%      RESULTS FROM THE MATLAB COMMAND "HELP 322EG1".
%
%      IT CAN ALSO BE USED TO MAKE PARTS OF THE PROGRAM NOT EXECUTE
%      TEMPORARILY.  THE LOWER PART OFTHIS PROGRAM IS CONSTRUCTED
%      IN THAT WAY.
%
%
%      NOW HERE COMES THE PART OF THIS PROGRAM THAT EXECUTED WHEN
%      YOU RAN IT BY TYPING THE COMMAND  s322eg1

outstr = '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' ;
                %  THIS IS A "STRING", ACTUALLY A VECTOR OF ASCII CHARACTERS
 
outstr = [outstr,10] ;
                %  THIS ADDS THE ASCII CODE 10, "NEW LINE", TO THE STRING
                %  NOTE THE FORMAT OF "COMBINING MATRICES"

outstr = [outstr,'!!!   Congratulations, you are running s322eg1  !!!',10] ;
outstr = [outstr,'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!',10] ;
                %  THESE ADD MORE LINES TO THE STRING

outstr = [outstr,7,10,7,10,7,10] ;
                %  7 IS THE ASCII CODE FOR "BEEP", COULD YOU HEAR ALL 3?
                %  TRY MODIFYING THE PROGRAM SO THAT IT ONLY BEEPS ONCE,
                %  AND SEE IF YOU CAN HEAR THE DIFFERENCE.
                %  AFTER EDITING A .m FILE, BE SURE TO SAVE BEFORE YOU
                %  RUN IT AGAIN.

outstr = [outstr,'Now edit the file s322eg1.m,'] ;
outstr = [outstr,' to see how this is done',10] ;

outstr
                %  THIS SAYS TAKE THE STRING CONSTRUCTED ABOVE
                %  AND WRITE IT ON THE SCREEN



%
%      ANOTHER WAY TO GET OUTPUT IS TO CREATE AN ASCII FILE, CONTAINING
%      WHAT YOU WANT, AND THEN COPYING IT TO THE PRINTER.
%
%      THE FIRST STEP TO CREATE SUCH A FILE IS TO "OPEN IT":

%fid = fopen('s322eg1.out','wt') ;
                %  REMOVE THE % AT THE BEGINNING OF THIS LINE TO DO THIS
                %  THE FIRST ARGUMENT IS A STRING WITH THE FILE NAME
                %  THE SECOND ARGUMENT HAS a FOR "APPEND MODE"
                %                      AND t FOR "TEXT FILE"
                %  TO SEE OTHER OPTIONS, TRY THE COMMAND  >> help fopen

%fid
                %  REMOVE THIS PERCENT TO SEE THAT fid IS AN INTEGER VALUE

%titlstr = 'Output from the MATLAB Script s322eg1.m,   ' ;
%titlstr = [titlstr,date] ;
                %  UNCOMMENT THESE LINES TO CREATE A STRING TO WRITE 
                %  TO THE OUTPUT FILE

%cntbytes = fprintf(fid,'%1s \n\n',titlstr) ;
                %  UNCOMMENT THIS LINE TO DO THE ACTUAL WRITING.
                %  fid IS THE "FILE IDENTIFIER DEFINED ABOVE
                %  %1s IS A C FORMAT STATEMENT
                %  \n SAYS PUT IN A LINE FEED
                %  cntbytes IS THE NUMBER OF BYTES WRITTEN (CAN BE USED
                %                   LATER IF NEEDED)
                %  TO LEARN MORE, USE  >> help fprintf

%a = rand(1,4) ;
                %  UNCOMMENT THIS LINE TO CREATE A 1X4 MATRIX OF
                %  PSEUDO RANDOM U(0,1) REALIZATIONS

%a
                %  UNCOMMENT THIS LINE TO SEE THE RESULT

%a = [a ; 100*rand(1,4) ; 10^10*rand(1,4)] ;
                %  UNCOMMENT THIS LINE TO MAKE A BIGGER

%a
                %  UNCOMMENT THIS LINE TO SEE THE RESULT
                %  NOTE HOW THE MATLAB STANDARD OUTPUT HANDLES
                %        ENTRIES WITH REALLY DIFFERENT MAGNITUDES

%format short e ;
%a
                %  UNCOMMENT THESE LINES FOR A MORE USEFUL WAY
                %  TO LOOK AT THE ENTRIES IN a

%cntbytes = fprintf(fid,'%6.4f %7.1f %12.4d %15.8e\n',a') ;
                %  UNCOMMENT THIS LINE TO WRITE a TO THE OUTPUT FILE.
                %  NOTE HOW THE DIFFERENT FORMATS WORK
                %  THE TRANSPOSE OF a IS USED, BECAUSE THIS COMMAND
                %  "READS DOWN THE COLUMNS OF a"

fclose('all')
                %  THIS LINE IS USEFUL AT THE END OF PROGRAMS THAT
                %  OPEN FILES, BECAUSE IT MAKES THE FILES AVAILABLE
                %  FOR OTHER PURPOSES, E.G. EDITING (TO TAKE A LOOK)

%      THAT IS IT FOR THIS PROGRAM.  IF YOU HAVE TROUBLE WITH IT,
%      COMMENTS ARE APPRECIATED.  IF YOU FEEL REALLY LOST, FIRST
%      TRY TO SORT IT OUT YOURSELF BY MAKING A PRINT OF THE 
%      PROGRAM FILE.
