From e0178658b031118ce61dc482abe00c7201280cac Mon Sep 17 00:00:00 2001
From: Jan Grewe <jan.grewe@g-node.org>
Date: Sun, 20 Dec 2020 13:44:25 +0100
Subject: [PATCH] [programming] add box on sprintf

---
 programming/lecture/programming-chapter.tex |  2 +-
 programming/lecture/programming.tex         | 34 +++++++++++++++++++--
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/programming/lecture/programming-chapter.tex b/programming/lecture/programming-chapter.tex
index 2a76f64..941c688 100644
--- a/programming/lecture/programming-chapter.tex
+++ b/programming/lecture/programming-chapter.tex
@@ -19,7 +19,7 @@
 \section{TODO}
 \begin{itemize}
 \item Ausgabeformat: \varcode{format} ?
-\item Expliziter die \varcode{(a:b:c)} Notation einf\"uhren!
+\item Expliziter die \varcode{(a:b:c)} Notation einf\"uhren! Done!
 \item Mathematische Funktionen sin(), cos(), exp()
 \item Rundungsfunktionen round(), floor(), ceil()
 \item Zeitvektoren, deltat, Umrechnung Zeit und Index.
diff --git a/programming/lecture/programming.tex b/programming/lecture/programming.tex
index e123739..6681f61 100644
--- a/programming/lecture/programming.tex
+++ b/programming/lecture/programming.tex
@@ -1749,6 +1749,36 @@ function plotFunction(x_data, y_data, name)
 end
 \end{lstlisting}
 
+\begin{ibox}[!ht]{\label{box:sprintf}\enterm[Format strings]{Formatted strings} with \mcode{sprintf}.}
+    We can use \varcode{disp} to dispaly the content of a variable on the command line. But how can we embed variable contents nicely into a text and store it in a variable or show it on the command line? \varcode{sprintf} is the standard function to achieve this. We introduce only a fraction of its real might here. For a more complete description visit the \matlab{} help or e.g. \url{https://en.wikipedia.org/wiki/Printf_format_string}. 
+    
+    Consider the following code:
+    \begin{lstlisting}[commentstyle=\color{black}, basicstyle=\ttfamily\scriptsize, caption={Using sprintf.}]
+n = 100;
+numbers = randn(n, 1);
+    
+sprintf("Drawing %i random numbers yields an average of %.3f and a standard deviation of %.3f.", n, mean(numbers), std(numbers))
+
+ans = 
+    "Drawing 100 random numbers yields an average of -0.121 and a standard deviation of 0.921."
+
+    \end{lstlisting}
+    \varcode{sprintf} takes a variable number of arguments. The first one is the string that contains placehoders for the variable content. Here we used three placehoders (two different types, \varcode{\%i, \%.2f}). Accordingly, \varcode{sprintf} expects three additional arguments (\varcode{n, mean(numbers), std(numbers)}). These are the content we want to show in the text. The function will try to convert the variable content and format is according to the placehoder type:
+
+   \footnotesize
+   \begin{tabular}{l||p{4cm}|p{7.5cm}}
+     type & purpose & example \\ \hline \hline
+    \%i & integer values & \varcode{sprintf("I drew \%i numbers", 100)}\\ \hline
+    \%s & string values & \varcode{name = "John Doe";} \varcode{sprintf("My name is \%s", name)}\\ \hline
+    \%f & double values, accepts width and precision arguments & \varcode{sprintf("Number pi is \%.2f", pi)}\\ \hline
+    \%g & double values, uses exponential style when appropriate& \varcode{sprintf("Large int \%g", 1000000)}
+    \end{tabular}
+   \normalsize
+
+    Simplified, the placehoder has the form \varcode{\%[width][.precision]type}. Square brackets indicates optional parameters. \varcode{width} controls the total number of characters used for the content. \varcode{.precision} controls how many of the total characters may de devoted to the positions after the decimal point. The example above (\varcode{\%.2f}) limits the output of the $\pi$ to 2 positions after the decimal point.
+
+    Many programming languages offer versions of \varcode{sprintf} that work essentially in the same way.
+\end{ibox}
 
 \paragraph{III. One script to rule them all}
 
@@ -1774,7 +1804,7 @@ Again, we need to specify what needs to be done:
 
 The implementation is shown in listing~\ref{sinesskriptlisting}.
 
-\begin{lstlisting}[caption={Control script for the plotting of sine waves.},label=sinesskriptlisting]
+\begin{lstlisting}[caption={Control script for the plotting of sine waves. Note the use of \varcode{sprintf} in the function call in line 11. This function creates a so called formatted string based on the passed string which contains placeholders for variables. For some more details see~\ref{box:sprintf}.},label=sinesskriptlisting]
 amplitudes = 0.25:0.25:1.25;
 frequency = 2.0;
 duration = 10.0;  % seconds
@@ -1785,7 +1815,7 @@ hold on
 for i = 1:length(amplitudes)
   [x_data, y_data] = sinewave(frequency, amplitudes(i), ... 
                               duration, stepsize);
-  plotFunction(x_data, y_data, sprintf('freq: %5.2f, ampl: %5.2f',...
+  plotFunction(x_data, y_data, sprintf("freq: %5.2f, ampl: %5.2f",...
                                        frequency, amplitudes(i)))
 end
 hold off