I want to create an XY plot, on a chart, on a specific worksheet in an Excel file, via MATLAB. The x and y values (for the XY plot) are already there on that specific worksheet of the Excel file. All I want to do is to create an XY plot on a chart, on the same worksheet of the Excel file. With help from MATLAB Forum a couple of weeks ago, I wrote a MATLAB script (as shown below). My script works well when I run it the first time. However, if I run my script the second time, another XY plot chart object will be created (on top of the first XY plot chart object) on the same worksheet in the Excel file. If I run my script multiple times (say a total of 5 times), I will end up with 5 identical XY plot chart objects on the same worksheet in the Excel file.
What shall I do to avoid this mistake?
Any comments and suggestions will be greatly appreciated!!!
P.S. For your review, the original Excel file (with the x and y values only, and no XY plot chart objects yet) is also attached.
% clear memory, clear Command Window
clear; clc;% set file path and file name for an Excel file
filename = 'practice.xlsx'; fullpathToExcelFile = [pwd '\' filename]; % name of the data sheet that I want to work on. The Excel file already has a work sheet named 'DataSheet'.
sheetName = 'DataSheet'; % create an object of Active-X COM server for Excel application
excelApp = actxserver('Excel.Application'); % create an Active-X COM server for Excel application
excelApp.DisplayAlerts = false; % turn alerts off. So we don't get the message: "The file already exists. Do you want to replace it?" all the time.
excelApp.Visible = true; % make this Excel application visible, so we can see what happens
% open an Excel file
myWorkBook = excelApp.workbooks.Open(fullpathToExcelFile); % open a workbooks object by using the specified filepath and filename. NOTE: Full path is required
% define my work sheets (i.e., get the "Sheets" object that contains all information related to sheets)
myWorkSheets = myWorkBook.Sheets; % define active worksheet
myWorkSheet = myWorkSheets.get('Item', sheetName); % get my worksheet number
myWorkSheet.Activate; % set it as the active worksheet
% create an object of ChartObjects
myChartObject = myWorkSheet.ChartObjects.Add(100, 30, 400, 250); % create an object of Chart.
myPlots = myChartObject.Chart; myPlots.HasTitle = true; myPlots.ChartTitle.Text = 'ECAP Growth Function'; % create an object of SeriesCollection (XY plot for the raw data)
line1 = myPlots.SeriesCollection.NewSeries; % speficy x and y values. The x values are on the cell range 'A2:A14', and the y values are on the cell range 'B2:B14' of the existing Excel file.
myPlots.SeriesCollection(1).XValue = myWorkSheet.Range('A2:A14'); myPlots.SeriesCollection(1).Values = myWorkSheet.Range('B2:B14'); line1.ChartType = 'xlXYScatter'; line1.Name = 'raw data'; % Set X-axis and Y-axis titles.
myChartObject.Chart.Axes(1,1).HasTitle = true;myChartObject.Chart.Axes(1,1).AxisTitle.Caption = 'Stimulus Intensity (cu)'; myChartObject.Chart.Axes(2,1).HasTitle = true;myChartObject.Chart.Axes(2,1).AxisTitle.Caption = 'ECAP Amplitude (uV)';% save and close Excel file
myWorkBook.SaveAs(fullpathToExcelFile); myWorkBook.Close; % quit and delete this Active-X COM Excel application object, and delete this object
excelApp.Quit; excelApp.delete;
Best Answer