How to set an axis as secondary in Excel with a Matlab-code

4 visualizzazioni (ultimi 30 giorni)
Dear Community,
I am new to Matlab and have no experience in VBA-programming. This is why I can't figure out how to set one Y-axis in an Excel-plot as a secondary axis (with a Matlab-script). I've got the following code from this site :
Excel = actxserver('Excel.Application');
ResultFile = [pwd '\ResultFile.xls'];
Workbook = invoke(Excel.Workbooks,'Open', ResultFile);
set(Excel,'Visible',1);
Chart = Excel.ActiveSheet.Shapes.AddChart;
%Let us Rename this chart to 'ExperimentChart'
Chart.Name = 'ExperimentChart';
%%Delete Default Entries
% Let us delete all the entries in the chart generated by defalut
ExpChart = Excel.ActiveSheet.ChartObjects('ExperimentChart');
ExpChart.Activate;
try
Series = invoke(Excel.ActiveChart,'SeriesCollection',1);
invoke(Series,'Delete');
Series = invoke(Excel.ActiveChart,'SeriesCollection',1);
invoke(Series,'Delete');
Series = invoke(Excel.ActiveChart,'SeriesCollection',1);
invoke(Series,'Delete');
catch e
end
%We are left with an empty chart now.
%Insert a Chart for Column B
NewSeries = invoke(Excel.ActiveChart.SeriesCollection,'NewSeries');
NewSeries.XValues = ['=' resultsheet '!A' int2str(2) ':A' int2str(16)];
NewSeries.Values = ['=' resultsheet '!B' int2str(2) ':B' int2str(16)];
NewSeries.Name = ['=' resultsheet '!B' int2str(1) ];
%Insert a Chart for Column C
NewSeries = invoke(Excel.ActiveChart.SeriesCollection,'NewSeries');
NewSeries.XValues = ['=' resultsheet '!A' int2str(2) ':A' int2str(16)];
NewSeries.Values = ['=' resultsheet '!C' int2str(2) ':C' int2str(16)];
NewSeries.Name = ['=' resultsheet '!C' int2str(1) ];
Excel.ActiveChart.ChartType = 'xlXYScatterLinesNoMarkers';
% Set the x-axis
Axes = invoke(Excel.ActiveChart,'Axes',1);
set(Axes,'HasTitle',1);
set(Axes.AxisTitle,'Caption','Experiment')
% Set the y-axis
Axes = invoke(Excel.ActiveChart,'Axes',2);
set(Axes,'HasTitle',1);
set(Axes.AxisTitle,'Caption','Results')
%Give the Chart a title
Excel.ActiveChart.HasTitle = 1;
Excel.ActiveChart.ChartTitle.Characters.Text = 'Result vs Experiment';
%%Chart Placement
Location = [ xlcolumn(2) int2str(20) ];
GetPlacement = get(Excel.ActiveSheet,'Range', Location);
% Resize the Chart
ExpChart.Width = 400;
ExpChart.Height= 250;
ExpChart.Left = GetPlacement.Left;
ExpChart.Top = GetPlacement.Top;
invoke(Excel.ActiveWorkbook,'Save');
Excel.Quit;
Excel.delete;
clear Excel;
How can i set the column C being plotted on a secondary y-axis?
Hope someone can help...

Risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by