MATLAB Fast Automation: Tired of Working? Here's Your Step-by-Step Action Plan to Automate Your Work With MATLAB.



After reading this article, you’ll know how to add a row of cells after the last line of your Excel file.

Let’s say you want to add the cell “New Value” at the end of the Excel file named emptyRows.xlsx:

To add a row after the last line of this file, you’ll first need to know the number of empty rows using getRowNumberXlsx (see previous article). This is done by identifying the row number of the first row with content, namely firstRowWithContent. The number of the row to which you would like to add content would be:

new row number = row number of the first row with content+number of rows with content


row number of the first row with content = number of empty rows+1

Then, you just have to write it on the Excel file using the MATLAB function xlswrite:

fileName = 'emptyRows.xlsx';
sheetNumber = 1;
fullPath = pwd;
columnNumber = 1;
value = 'firstRowWithContent';
rowNumberOfFirstRowWithContent = getRowNumberXlsx(fileName, ...
    sheetNumber, fullPath, columnNumber, value);
[~, txt, ~] = xlsread(fileName);
newRowNumber = rowNumberOfFirstRowWithContent + size(txt, 1);
range = ['A' num2str(newRowNumber) ':A' num2str(newRowNumber)];
xlswrite(fileName, {'New Value'}, range)

We get:

And, if you run the script twice in a row:

Leave a Reply