[8] | 1 | %'cell2tab': transform a Matlab cell in a character array suitable for display in a table
|
---|
[89] | 2 | %------------------------------------------------------------------------
|
---|
| 3 | % function Tabchar=cell2tab(Tabcell,separator)
|
---|
| 4 | %
|
---|
| 5 | % OUTPUT:
|
---|
[128] | 6 | % Tabchar: column cell of char strings suitable for display (equal length)
|
---|
[89] | 7 | %
|
---|
[8] | 8 | % INPUT:
|
---|
[128] | 9 | % Tabcell: (ni,nj) cell matrix of char strings to be displayed as ni lines , nj column
|
---|
| 10 | % separator: char string used for separating displayed columns
|
---|
[89] | 11 |
|
---|
[8] | 12 | function Tabchar=cell2tab(Tabcell,separator)
|
---|
[125] | 13 | [ni,nj]=size(Tabcell);
|
---|
| 14 |
|
---|
| 15 | %determine width of each column
|
---|
[128] | 16 | if isequal(ni,1)
|
---|
| 17 | widthcolumn=cellfun('length',Tabcell);% case of a single line, no justification used
|
---|
| 18 | else
|
---|
| 19 | widthcolumn=max(cellfun('length',Tabcell));
|
---|
| 20 | end
|
---|
| 21 | lsep=numel(separator); %nbre of characters of the separator
|
---|
| 22 | nbchar_line=(sum(widthcolumn)+(nj-1)*lsep); %total nbre of characters in each output line
|
---|
| 23 | default_line=blanks(nbchar_line); %default blank line
|
---|
| 24 | Tabmat=reshape(blanks(nbchar_line*ni),ni,nbchar_line);
|
---|
| 25 | Tabchar=mat2cell(Tabmat,ones(1,ni),nbchar_line); %default output
|
---|
[125] | 26 |
|
---|
[8] | 27 | %justify table
|
---|
[125] | 28 | for itab=1:ni
|
---|
[128] | 29 | charchain=default_line;
|
---|
[125] | 30 | for jtab=1:nj% read line
|
---|
[8] | 31 | textlu=Tabcell{itab,jtab};
|
---|
[128] | 32 | if jtab==1
|
---|
| 33 | charchain(1:length(textlu))=textlu;%introduce separator chain string except for the first column
|
---|
| 34 | ind_column=widthcolumn(1);%new current char index in the line
|
---|
| 35 | else
|
---|
| 36 | charchain(ind_column+1:ind_column+lsep)=separator;%introduce separator chain string except for the first column
|
---|
| 37 | charchain(ind_column+lsep+1:ind_column+lsep+length(textlu))=textlu;%introduce separator chain string except for the first column
|
---|
| 38 | ind_column=ind_column+widthcolumn(jtab)+lsep;
|
---|
[8] | 39 | end
|
---|
| 40 | end
|
---|
| 41 | Tabchar(itab,1)={charchain};
|
---|
[125] | 42 | end
|
---|