How to delete cells in Excel? Using Matlab to create a chart adds a graph with a makred area value

Excel 2016 remembers the last cells you "marked" (selected) before closing the file, and the same asrea will be marked the next time you open the file.

The only way to not mark the area is with an untouched file generated by Matlab for example.

How to delete all areas in Excel using Matlab?

In my program (code below) I am accessing an existing Excel file using Matlab. The code creates a new chart with the specified range.

Unfortunately, there are always more graphs and a range selector in a chart. To be the more Persian area selected in the matlab file, a new plot will be added. (when it's empty an empty graph will be added anyway)

here you can see the marked area: here you can see the marked area

which after running the code will be added as a graph with exactly the specified range marked: new unnamed schedule range


original program with code and information to understand the project:

Original goals:

  • Automatic creation of a diagram in an already existing file .xlsx

    using Matlab
  • Easy way to select Range, X- and Y-values ​​(by sheet and column) freely
  • applicable to all sizes in files .xlsx

    . (up to 20,000 rows and 50 columns and multiple sheets).

Common problems:

  • untouched files work
  • in every other file additional graphics are added

code:

% Start Excel and open workbook
Excel = actxserver('Excel.Application');  
WB = Excel.Workbooks.Open('C:\...\test.xlsx');
%the workbook
Excel.visible = 1;
% Add chart
Chart = invoke(WB.Charts,'Add');
% Get Sheet object
SheetObj = Excel.Worksheets.get('Item', 'Tabelle1'); % 'Tabelle1' is german Ecel equation for 'sheet1'
%% FORMATTING %%
% Name chart sheet
Chart.Name = 'TestChart';
% Set chart type
Chart.ChartType = 'xlXYScatterSmoothNoMarkers'; 
% Set chart title,  see https://msdn.microsoft.com/en-us/library/office/ff196832.aspx
Chart.HasTitle = true;
Chart.ChartTitle.Text = 'Test Title';
% Set chart legend, see https://msdn.microsoft.com/en-us/library/office/ff821884.aspx
Chart.HasLegend = true;
%% Set source data range of chart, do X and Y data for each series    
columns = 3:7; % set columns 3 to 7 as Y-Values
%%
colnames = {'xdata', 'my series 1', 'my series 2', 'my series 3','my series 4','my series 5','my series 6', 'my series 7', 'my series 8','my series 9'};
for col = columns
      %% Get Excel column *letter* from column *number*
    colchar = strrep([char(96+floor((col-1)/26)) char(97+rem(col-1,26))],char(96),'');
     % Last row of data, see https://msdn.microsoft.com/en-us/library/office/ff839539.aspx
      %% Data must be contiguous (no gaps / blank cells)
    lastrow = num2str(SheetObj.Range([colchar, '2']).End('xlDown').Row);
      %% Y data, creating range strings by concatenation of column character and row number
    Srs = Chart.SeriesCollection.Add(SheetObj.Range([colchar, '2:', colchar, lastrow]));
      %% X data, same approach is used for last row, but only column = 1 = "A" 
    Srs.XValues = SheetObj.Range(['A2:A', lastrow]); % set column A as X-Value
     %% Set column name

    Srs.Name = colnames{col};
end

      

What the code does:

When it works , a new chart is created which has Column A as X-Value and Column 3-7 as Y-values ​​from colnames

Column.

When it is not working , it does the same thing as it does when it is working, but adds more graphs to the chart. More precisely, it adds each column of the file as a Y-Value without an X-Value (so 1,2,3,4 ...), using the first row as its names.

Summary of the question

  • Is there a way to uncheck, unselect areas in Excel using Matlab code?
  • You know where the problem comes from
  • any helpful solutions
+2


source to share


1 answer


I found a solution. Or more ways to solve this problem.

So again: The problem is that the pre-selected cells will automatically appear in your graph even before you select your own data and X- and Y-values.

Decision:

clear the graph between the steps to automatically create a graph and select your own data



to do this, you just need to put this one-liner after SheetObj = Excel.Worksheets.get('Item', 'Tabelle1');

:

%% Clear content 
Chart.ChartArea.ClearContents;

      

to keep your chart blank before you select data to fill it in.

It worked for me ... Hope it helps you too.

0


source







All Articles