Working with a group of researchers can be very interesting but also very frustrating if you have to use their software tools. In my case, I have to use Matlab. If you are used to R, Python, and Gams this is more than a nightmare. This post is after a frustrating day with Matlab.
Most of my researcher colleagues use in their Matlab code matrices, so no row names or column headings. What you see are just numbers. If you want to know what the number represents, you need to look at vectors that contain the sets/row names/headers. Some of my colleagues have mercy with me and use (like I do) tables. However, that is not the end of the frustration. If you want to define a table, you write something like
1 |
mytable = table(matrix, 'RowNames', vector_with_rownames, ....) |
Now, if you believe that adding column names would need something like
1 |
'ColNames', vector_with_colnames |
you are wrong. You should use ‘VariableNames’.
Other stuff that is frustrating:
- Reading from an excel sheet using xlsread: if you have some empty cells at the beginning of the last row, Matlab just leaves them out. In order to have this line read properly, you must use the following three lines of code (look it up in the documentation and there is no clear indication that you only can read numbers directly without NaN directly:
1 2 3 |
[m_use_tot,txt,raw] = xlsread(excelfile,'sut_bal','CA2..FU161'); and raw(cellfun(@ischar,raw)) = {NaN}; % Set non-scalar values to missing. A = cell2mat(raw); % Convert to matrix. |
- Export a table to excel: the column headings from the table are used, but the row names not. The row names have to be exported separately.
The good thing: you really learn to love R, Python, and Gams more than anything else. For Matlab users: never look over the shoulder of someone using other software 🙂