Using cell offset in Excel sheet from Matlab
5 ビュー (過去 30 日間)
古いコメントを表示
ExcelFileName = 'Results.xls';
% COM Object for Excel application
h = actxserver('Excel.Application');
set(h, 'DisplayAlerts', 0);
set(h, 'visible', 0);
exlWkbk = h.Workbooks;
exlOpen = exlWkbk.Open([pwd,'\',ExcelFileName], 0, false);
exlSheets = h.ActiveWorkbook.Sheets;
exlsheetObj = exlSheets.get('Item', 'Header');
exlsheetObj.Activate;
Addr = exlsheetObj.Range('B:B').Find('Renish').Address;
ActiveRange = get(h.Activesheet, 'Range', Addr);
ActiveRange.Select;
ActiveRange.Offset(1,1).Value;
With this code, I get the value in the cell as 'Renish'. When I need to get the value of neighbouring cell, I used offset as in VB script
ActiveRange.Offset(1,2).Value;
But it throws error as "Index exceeds matrix dimensions."
If you specify "Offset(1,0)" or "Offset(1,-1)" in the same code, it throws error as "Subscript indices must either be real positive integers or logicals."
Can anybody help me to find the values of neighbouring cells.
Also how can "Subscript indices" be given "logicals"? Is it a incorrect information display from mathworks?
I even tried below steps and nothing is working
h.ActiveCell.Offset(1,2).Value;
h.Selection.Offset(1,2).Value;
0 件のコメント
回答 (1 件)
Andreas Martin
2017 年 5 月 22 日
編集済み: Andreas Martin
2017 年 5 月 22 日
Hi,
maybe it's outdated, but for completeness: using the function 'get' does the trick.
ExcelFileName = 'Results.xls';
% COM Object for Excel application
h = actxserver('Excel.Application');
set(h, 'DisplayAlerts', 0);
set(h, 'visible', 0);
exlWkbk = h.Workbooks;
exlOpen = exlWkbk.Open([pwd,'\',ExcelFileName], 0, false);
exlSheets = h.ActiveWorkbook.Sheets;
exlsheetObj = exlSheets.get('Item', 'Header');
exlsheetObj.Activate;
Addr = exlsheetObj.Range('B:B').Find('Renish').Address;
ActiveRange = get(h.Activesheet, 'Range', Addr);
ActiveRange.Select;
ActiveRange. *get*( 'Offset', 1, 1 ).Value; % ActiveRange.Offset(1,1).Value;
I think some calls are superfluous (and time expensive) and may be omitted:
ExcelFileName = 'Results.xls';
% COM Object for Excel application
h = actxserver('Excel.Application');
set(h, 'DisplayAlerts', 0);
set(h, 'visible', 0);
exlWkbk = h.Workbooks;
exlOpen = exlWkbk.Open([pwd,'\',ExcelFileName], 0, false);
exlSheets = h.ActiveWorkbook.Sheets;
exlsheetObj = exlSheets.get('Item', 'Header');
Cell = exlsheetObj.Range('B:B').Find('Renish');
value = Cell.get( 'Offset', 1, 1 ).Value;
Cheers, Andreas
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Use COM Objects in MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!