Matlab number of rows in excel file

房东的猫 提交于 2019-12-10 14:29:37

问题


is there a command of Matlab to get the number of the written rows in excel file? firstly, I fill the first row. and then I want to add another rows in the excel file. so this is my excel file:

I tried:

e = actxserver ('Excel.Application');
filename = fullfile(pwd,'example2.xlsx');
ewb = e.Workbooks.Open(filename);
esh = ewb.ActiveSheet;

sheetObj = e.Worksheets.get('Item', 'Sheet1');
num_rows = sheetObj.Range('A1').End('xlDown').Row

But num_rows = 1048576, instead of 1. please help, thank you!


回答1:


If the file is empty, or contains data in only one row, then .End('xlDown').Row; will move to the very bottom of the sheet (1048576 is the number of rows in a Excel 2007+ sheet).

Test if cell A2 is empty first, and return 0 if it is.

Or use Up from the bottom of the sheet

num_rows = sheetObj.Cells(sheetObj.Rows.Count, 1).End('xlUp').Row 

Note: I'm not sure of the Matlab syntax, so this may need some adjusting




回答2:


You can use MATLAB's xlsread function to read in the spreadsheet. This obtains the following fields:

[numbers strings misc] = xlsread('myfile.xlsx');

if you do a size check on strings or misc, this should give you the following:

[rows columns] = size(strings);

testing this, I got rows = 1, columns = 10 (assuming nothing else was beyond 'A' in the spreadsheet).



来源:https://stackoverflow.com/questions/10158090/matlab-number-of-rows-in-excel-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!