Search code examples
excelmatlabactivex

Excel MathType Equation from matlab


Is it possible to insert a mathType equation from matlab into the excel spreadsheet?

I am going to create an excel file through matlab and create a pdf report. My report includes some formula and I want to create them with excel MathType equation. Is there any access to MathType equation from activeX in matlab?


Solution

  • This would require creating a MathType equation, then adding it as an object to the Excel file through ActiveX. I haven't found any way to call MathType from MATLAB, let alone move the resulting object to the file.

    One alternative is to plot your formula in a MATLAB figure, then capture and insert the figure as an image into your Excel file through ActiveX. Here's an example for plotting the formula:

    hFigure = figure('Position', [100 100 300 200], 'Color', 'w');   % Create white figure
    hAxes = axes(hFigure, 'Position', [0 0 1 1], 'Visible', 'off');  % Create invisible axes
    text(hAxes, 0.5, 0.5, '$$y = \sum_{i=1}^N a_i*x_i$$', ...        % Create equation text
         'Interpreter', 'latex', ...
         'FontSize', 30, ...
         'HorizontalAlignment', 'center');
    

    And the resulting figure:

    enter image description here

    Now you can print this figure to the clipboard and insert it into an Excel worksheet with ActiveX:

    excel = actxserver('Excel.Application');  % Create server object
    excelWorkbook = excel.Workbooks.Add(1);   % Add a workbook
    excelSheet = excel.ActiveSheet;           % Get the active sheet
    dpi = get(groot, 'ScreenPixelsPerInch');  % Get screen dpi
    print(hFigure, sprintf('-r%d', dpi), ...  % Print the figure at the screen resolution
          '-clipboard', '-dbitmap');          %   to the clipboard as a bitmap
    excelSheet.Range('B2').PasteSpecial();    % Paste from clipboard (top left corner
                                              %   of image will be in the cell 'B2')
    excelWorkbook.SaveAs('formula.xlsx');     % Save workbook to a file
    excelWorkbook.Close();                    % Close workbook
    excel.Quit();                             % Quit server
    excel.delete();                           % Delete server object
    

    And here's how it looks in the Excel file:

    enter image description here