diff --git a/programming/lecture/programming.tex b/programming/lecture/programming.tex index 44c025e..e123739 100644 --- a/programming/lecture/programming.tex +++ b/programming/lecture/programming.tex @@ -176,11 +176,12 @@ As mentioned above, the data type associated with a variable defines how the sto part. \item \enterm{logical}: Boolean values that can be evaluated to \code{true} or \code{false}. -\item \enterm{character}: ASCII characters. +\item \enterm{character}: Unicode characters. +\item \enterm{string}: Strings of unicode characters, i.e. text. \end{itemize} There is a variety of numeric data types that require different amounts of memory and have different ranges of values that can be -represented (table~\ref{dtypestab}). +represented (table~\ref{dtypestab}). There are also more advanced data types which will not be used in this chapter but see box~\ref{box:advanced_dtypes} and box about strings \ref{box:strings}. \begin{table}[t] \centering @@ -1156,7 +1157,7 @@ segment of data of a certain time span (the stimulus was on, \end{itemize} \end{exercise} -\begin{ibox}[ht]{\label{advancedtypesbox}Advanced data types} +\begin{ibox}[!ht]{\label{advancedtypesbox}Advanced data types} Thoughout this script and the exercises we will limit ourselves to the basic data types introduced above (int, double, char, scalars, vectors, matrices and strings). There are, however, \matlab{}- @@ -1212,6 +1213,50 @@ ans = \end{ibox} +\begin{ibox}[!ht]{\label{box:string_dtype}The \enterm[string]{string} data type} + With release 2017a \matlab{} introduced the string data type which offers more convenience for handling text. A string scalar is created using double quotes: \varcode{s = "Hello world!";}. + The string object \varcode{s} now offers several methods to work on the stored information. Some of the most commonly used methods are listed below. Refer to the documentation for complete descriptions. + \begin{enumerate} + \item \varcode{append} -- appends another string to the string and returns the new string. As with the other functions the original is not affected.\vspace{-0.5em} + \item \varcode{contains} -- searches the string for the occurence of a another string, which is passed as an argument. Returns a logical that is true if the pattern was found.\vspace{-0.5em} + \item \varcode{split} -- splits the string into substrings. By default a blank is used as delimiter. You can pass any other string as an argument to spilt the string at each occurence of the passed pattern. Returns a vector of substrings.\vspace{-0.5em} + \item \varcode{join} -- The opposite of spilt. \varcode{join} takes a vector of strings and concatenates them to a single string. By default a blank is used as delimiter.\vspace{-0.5em} + \item \varcode{lower} -- returns a version of the string in which all characters are lower case. This is particularly useful when one compares user input or string arguments in a function in an case insensitive way.\vspace{-0.5em} + \item \varcode{upper} -- as above but all characters are converted to upper case. + \end{enumerate} + \paragraph{Example:} + A dataset is recorded on a specific recording date using an experimental subject and applying certain stimulus conditions. The recorded data should be stored in a file that contains this information separated by undersores. + + \begin{lstlisting}[basicstyle=\ttfamily\scriptsize, caption={Using \varcode{join} to combine strings.}, label=usingjoin] +date = "2020-12-24"; +subject = "subjectA"; +condition = "control"; +file_extension = ".dat"; + +filename = join([date, subject, condition], "_"); +filename = filename.append(file_extension) + +disp(filename) + +ans = + "2020-12-24_subjectA_control.dat" + \end{lstlisting} + + When reading the dataset during the analysis, we may want to extract the stimulus condtion or the subject name. + + \begin{lstlisting}[basicstyle=\ttfamily\scriptsize, caption={Using \varcode{split} to cut a string at a delimiter.}, label=usingsplit] +disp(filename) +ans = + "2020-12-24_subjectA_control.dat" + +filename = filename.erase(".dat"); % remove file extension +string_parts = filename.split("_"); % split at the underscore +% string_parts is a vector of the substrings +date = string_parts(1); +subject = string_parts(2); +condition = string_parts(3); + \end{lstlisting} +\end{ibox} \section{Control flow}\label{controlstructsec} Generally, a program is executed line by line from top to