Search code examples
vbamatlabcode-conversion

Excel VBA to MATLAB conversion to create a diagram with one Y axis and two X axes


I'd like to convert a diagram-creating-Excel-VBA Code to a Matlab script, so that I can manipulate an Excel file without dealing with excel, and by the Matlab script.Thanks in advance. VBA code is as follows:
Sub qwer() ' ' qwer Makro ' This macro creates a diagram with an X axis and two Y axes Sheets("Loadcase1").Select Range("D39:E39").Select Selection.Copy Sheets("Overview").Select Range("B60").Select ActiveSheet.Paste Range("H15").Select ActiveSheet.Shapes.AddChart.Select ActiveChart.ChartType = xlXYScatterSmooth Application.CutCopyMode = False ActiveChart.SeriesCollection.NewSeries ActiveChart.SeriesCollection(1).Name = "=""Min Film Thickness mean""" ActiveChart.SeriesCollection(1).XValues = "=Overview!$C$29:$H$29" ActiveChart.SeriesCollection(1).Values = "=Overview!$C$40:$H$40" ActiveChart.SeriesCollection.NewSeries ActiveChart.SeriesCollection(2).Name = "=""Sommerfeld Number""" ActiveChart.SeriesCollection(2).XValues = "=Overview!$C$29:$H$29" ActiveChart.SeriesCollection(2).Values = "=Overview!$C$43:$H$43" ActiveChart.SeriesCollection.NewSeries ActiveChart.SeriesCollection(3).Name = "=""Min Film Thickness Min""" ActiveChart.SeriesCollection(3).XValues = "=Overview!$C$29:$H$29" ActiveChart.SeriesCollection(3).Values = "=Overview!$B$60:$B$65" ActiveChart.SeriesCollection.NewSeries ActiveChart.SeriesCollection(4).Name = "=""Min Film Thickness Max""" ActiveChart.SeriesCollection(4).XValues = "=Overview!$C$29:$H$29" ActiveChart.SeriesCollection(4).Values = "=Overview!$C$60:$C$65" ActiveChart.SeriesCollection(2).Select ActiveChart.SeriesCollection(2).AxisGroup = 2 End Sub


Solution

  • I found a solution to my own question. Regarding matlab code is as follows:

    %% Excel Diagramm Versuch2
    clc; clear all; close all;
    excel = actxserver('excel.application');
    excel.Visible = 1;
    wkbk = excel.Workbooks.Open('Z:\...file directory...\V1.xlsx');
    wkbk.ActiveSheet;
    wkbk.ActiveSheet.Shapes.AddChart.Select;
    wkbk.ActiveChart.ChartType = 'XlXYScatterSmoothNoMarkers';
    % qwer makro Anfang
    wkbk.Sheet.Item('Tabelle2').Select;
    wkbk.Sheet.Item('Tabelle1').Select;
    wkbk.ActiveChart;
    % Set the x-axis
    Axes = invoke(excel.ActiveChart,'Axes',1); 
    set(Axes,'HasTitle',1); 
    set(Axes.AxisTitle,'Caption','Revolution[1/min]')
    % Set the y-axis
    Axes = invoke(excel.ActiveChart,'Axes',2); 
    set(Axes,'HasTitle',1); 
    set(Axes.AxisTitle,'Caption','Film Thickness[µm]')
    % Give the Chart a title 
    excel.ActiveChart.HasTitle = 1; 
    excel.ActiveChart.ChartTitle.Characters.Text = 'Film Thickness-Revolution';
    invoke(excel.ActiveChart.SeriesCollection,'NewSeries');
    wkbk.ActiveChart.SeriesCollection(1).Name = 'Min Film Thickness mean';
    wkbk.ActiveChart.SeriesCollection(1).XValues = 'Tabelle1!$A$5:$A$10';
    wkbk.ActiveChart.SeriesCollection(1).Values = 'Tabelle1!$B$5:$B$10';
    invoke(excel.ActiveChart.SeriesCollection,'NewSeries');
    wkbk.ActiveChart.SeriesCollection(2).Name = 'Sommerfeld Number';
    wkbk.ActiveChart.SeriesCollection(2).XValues = 'Tabelle2!$A$5:$A$10';
    wkbk.ActiveChart.SeriesCollection(2).Values = 'Tabelle2!$B$5:$B$10';
    wkbk.ActiveChart.SeriesCollection(2).AxisGroup = 2;
    pause(20)
    wkbk.Close;
    excel.Quit; 
    close excel