[programming] add box on sprintf

This commit is contained in:
Jan Grewe 2020-12-20 13:44:25 +01:00
parent 591c998228
commit e0178658b0
2 changed files with 33 additions and 3 deletions

View File

@ -19,7 +19,7 @@
\section{TODO} \section{TODO}
\begin{itemize} \begin{itemize}
\item Ausgabeformat: \varcode{format} ? \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 Mathematische Funktionen sin(), cos(), exp()
\item Rundungsfunktionen round(), floor(), ceil() \item Rundungsfunktionen round(), floor(), ceil()
\item Zeitvektoren, deltat, Umrechnung Zeit und Index. \item Zeitvektoren, deltat, Umrechnung Zeit und Index.

View File

@ -1749,6 +1749,36 @@ function plotFunction(x_data, y_data, name)
end end
\end{lstlisting} \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} \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}. 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; amplitudes = 0.25:0.25:1.25;
frequency = 2.0; frequency = 2.0;
duration = 10.0; % seconds duration = 10.0; % seconds
@ -1785,7 +1815,7 @@ hold on
for i = 1:length(amplitudes) for i = 1:length(amplitudes)
[x_data, y_data] = sinewave(frequency, amplitudes(i), ... [x_data, y_data] = sinewave(frequency, amplitudes(i), ...
duration, stepsize); 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))) frequency, amplitudes(i)))
end end
hold off hold off