Frage

I have an uitable whose header's title (variable "header") uses html code. I want to export the uitable data (included its header) to an Excel spreadsheet. This is the code written for this task:

%# header = get(htable,'ColumnName');
header = {'<html><center>Component X<br />(km/s<sup>2</sup>)</center></html>', ...
           '<html><center>Component Y<br />(km/s<sup>2</sup>)</center></html>', ...
           '<html><center>Component Z<br />(km/s<sup>2</sup>)</center></html>'},
numeric_data = rand(3,3);
data_Matrix = [header ; num2cell(numeric_data)],
xlswrite('file.xls',data_Matrix);

However, I can not get Excel spreadsheet header title appears written correctly.

War es hilfreich?

Lösung

You can't insert HTML into an Excel cell like that (well you can, but as you've discovered it won't be rendered, it will just display the HTML).

In your specific case, you can make use of a little Unicode instead of HTML to display your titles both in the uitable and in Excel. Note that 00B2 is Unicode for superscript 2 - See Wikipedia for a list of other characters.

>> numeric_data = rand(3,3);
>> header2 = {['Component X (km/s',char(hex2dec('00B2')),')'],...
        ['Component Y (km/s',char(hex2dec('00B2')),')'],...
        ['Component Z (km/s',char(hex2dec('00B2')),')']}
header2 = 
    'Component X (km/s²)'    'Component Y (km/s²)'    'Component Z (km/s²)'
>> htable2 = uitable('ColumnName',header2);
>> data_Matrix = [header2 ; num2cell(numeric_data)]
data_Matrix = 
    'Component X (km/s²)'    'Component Y (km/s²)'    'Component Z (km/s²)'
    [            0.50095]    [            0.28778]    [            0.88857]
    [            0.33155]    [            0.50127]    [            0.62051]
    [              0.243]    [            0.89398]    [             0.6544]
>> xlswrite('file.xls',data_Matrix);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top