MatLab to Excel

Sometimes I have trouble visualizing some matrix structures that I create in MatLab. And when I get to see them in the MatLab command line or the matlab workspace it does not help much. A way to deal with that and seek and understand patterns easier is to export them in excel. MatLab supports this with the instructions:

xlsread (to read from an excel file) and

xlswrite (to write to one)

the parameters that follow are:

(filename, matrix(to be written max 2 dimensions), sheet, range)

by using the following one can create matrixes that contain both data and titles:

matrix1 = {‘Title1', Title2'; 1 423 ; 2 742; 3 192}

And to actually write in an excel file:

xlswrite('matrix1.xls', matrix1, 'Example Sheet', 'E1')

It is needless to point out the usefulness of this. One can either import data to MatLab from an excel (which amy be automated to produce desired data and easily editable) and once the simulation is done and the results are calculated they can be exported for further analysis in an excel too.

Both tools are very powerful mathematical tools each one with different properties. The combination of these both can really give solutions to many problems!

Tables in LaTeX

I spent many hours today trying to figure out how I can produce a seeming simple table in LaTeX. The table should be like this:


Simple enough? My first trouble are the cells at the two last columns which are merged and the 4th column which has a lot of text. Another problem I had was centering the values in the tables vertically. The last problem was that the table was very big to fit in a single page. All of these problems led to a quest of discovery of many LaTeX packages. First of all creating a simple table:

\begin{tabular}{|c|c|c|} \hline
column1 - row1 & column2 - row1 & column3 - row1 \\ \hline 
column1 - row2 & column2 - row2 & column3 - row2 \\ \hline 
column1 - row3 & column2 - row3 & column3 - row3 \\ \hline 
\caption{Caption} \label{table:sample} 

which produces the following table:


The field next to \tabular defines the columns. ‘c’ stands for centered and the ‘|’ means that there will be a line between each column. The columns are separated by the ‘&’ used in each column. Caption declares the caption text and the label is used for other purposes not mentioned here. \hline is responsible for the horizontal lines. \cline{range} can be used to create horizontal lines under only a specific range. To merge cells, use the \multicolumn{columns}{position}{text} (for horizontal) or the \multirow{rows}{width}{text} (for vertical). To use these commands the multirow package should be added to the preamble \usepackage{multirow} , so we can easily produce the following table using the code:

\begin{tabular}{|c|c|c|} \hline
& \multicolumn{2}{|c|}{title} \\ \hline
\multirow{2}{*}{data} & 23.5 & 47.2 \\ \cline{2-3} 
& 26.9 & 54.9 \\ \hline 

which produces:


What is causing trouble though is that the data is not aligned in the center vertically. If data was a long text, the values would be stuck on the ceiling and the table would look really ugly:


In the above, the only thing that changes is the text in “data”. If the text spans beyond the page width, it is truncated (true story!).


Let’s try a more elegant solution with the values in the ceiling and the cell dimensions manually defined. The width is set by replacing the ‘*’ in the \multirow command with ‘1.8in’ (1.8 inches) and the height by adding [0.5in] after the ‘\\’ (which declares a new line). To center the values, the array package should be used: \usepackage{array} . This allows the use of m{width} to set specific column width along with ‘c’ (center) ‘l’ (flushleft) and ‘r’ (flushright). The m{width} also centers the cell vertically. For example:

\begin{tabular}{|>{\centerline}m{1.8in}|>{\centerline}m{0.5in}|} \hline

The >{\centerline} can be used with any text formatting for entire columns right before defining the \m{width} positioning. Finally, instead of using the \tabular environment, one can use the longtable package (\usepackage{longtable}). It enables the definition of the first head ending of the table, using \endfirsthead, the head ending of each page, using \endhead and finally, the ending of the table at each page using \endfoot. That’s all for now.