MATLAB xlswrite 名称数据
MATLAB xlswrite name data
如何使用 xlswrite 为从 MATLAB 写入 Excel 的矩阵指定名称?
这相当于打开xls sheet,选择数据,右击->定义名称。在这里我可以为矩阵分配一个字符串。我如何从 MATLAB 执行此操作?
我使用的另一个应用程序根据命名范围从 xls 文件中读取数据。
据我所知,您无法通过函数 xlswrite
执行此操作。您必须在 Matlab 中使用 excel COM 服务器。
下面的示例源自 Mathworks 示例,但已根据您的需要进行了调整。
请注意,您不需要以这种方式使用 xlswrite
,您可以直接将数据输入 excel sheet.
%// First, open an Excel Server.
e = actxserver('Excel.Application');
%// Insert a new workbook.
eWorkbook = e.Workbooks.Add;
e.Visible = 1;
%// Make the first sheet active.
eSheets = e.ActiveWorkbook.Sheets;
eSheet1 = eSheets.get('Item', 1);
eSheet1.Activate;
%// Name the range you want:
e.Names.Add( 'TestRange' , '=Sheet1!$A:$B' ) ;
%// since you are here, directly put the MATLAB array into the range.
A = [1 2; 3 4];
eActivesheetRange = e.Activesheet.get('Range', 'TestRange');
eActivesheetRange.Value = A ;
%// Now, save the workbook.
eWorkbook.SaveAs('myfile.xls');
编辑以在评论中回答您的问题:
如果要以编程方式定义范围地址,则必须将地址构建为字符串。
行索引很简单,但是 Matlab 中的 excel activeX 不允许通过索引引用 cells/column/range,因此要获得正确的列地址(以防溢出AAA...
个范围。
下面的位应该可以让你这样做:
%// test data
A = rand(4,32);
%// input the upper left start of the range you want
sheetName = 'Sheet1' ;
startCol = 'Y' ;
startRow = 4 ;
%// calculate the end coordinates of the range
endRow = startRow + size(A,1)-1 ;
%// a bit more fiddling for the columns, as they could have many "characters"
cols = double(startCol)-64 ;
colIndex = sum(cols .* 26.^(length(cols)-1:-1:0)) ; %// index of starting column
endCol = eSheet1.Columns.Item(colIndex+size(A,2)-1).Address ;
endCol = endCol(2:strfind(endCol, ':')-1) ; %// column string reference
%// build range string in excel style
rngString = sprintf('=%s!$%s$%d:$%s$%d',sheetName,startCol,startRow,endCol,endRow) ;
myRange = eSheet1.Range(rngString) ; %// reference a range object
myRange.Value = A ; %// assign your values
%// Add the named range
e.Names.Add( 'TestRange' , myRange ) ;
测试数据故意从 Y
列溢出到 BD
列,以确保地址转换可以处理 (i) 添加一个字符,以及 (ii) 从 Ax
溢出] 到 By
。这似乎工作得很好,但如果你的数据数组不是那么宽,那么你也不必担心它。
如何使用 xlswrite 为从 MATLAB 写入 Excel 的矩阵指定名称?
这相当于打开xls sheet,选择数据,右击->定义名称。在这里我可以为矩阵分配一个字符串。我如何从 MATLAB 执行此操作?
我使用的另一个应用程序根据命名范围从 xls 文件中读取数据。
据我所知,您无法通过函数 xlswrite
执行此操作。您必须在 Matlab 中使用 excel COM 服务器。
下面的示例源自 Mathworks 示例,但已根据您的需要进行了调整。
请注意,您不需要以这种方式使用 xlswrite
,您可以直接将数据输入 excel sheet.
%// First, open an Excel Server.
e = actxserver('Excel.Application');
%// Insert a new workbook.
eWorkbook = e.Workbooks.Add;
e.Visible = 1;
%// Make the first sheet active.
eSheets = e.ActiveWorkbook.Sheets;
eSheet1 = eSheets.get('Item', 1);
eSheet1.Activate;
%// Name the range you want:
e.Names.Add( 'TestRange' , '=Sheet1!$A:$B' ) ;
%// since you are here, directly put the MATLAB array into the range.
A = [1 2; 3 4];
eActivesheetRange = e.Activesheet.get('Range', 'TestRange');
eActivesheetRange.Value = A ;
%// Now, save the workbook.
eWorkbook.SaveAs('myfile.xls');
编辑以在评论中回答您的问题:
如果要以编程方式定义范围地址,则必须将地址构建为字符串。
行索引很简单,但是 Matlab 中的 excel activeX 不允许通过索引引用 cells/column/range,因此要获得正确的列地址(以防溢出AAA...
个范围。
下面的位应该可以让你这样做:
%// test data
A = rand(4,32);
%// input the upper left start of the range you want
sheetName = 'Sheet1' ;
startCol = 'Y' ;
startRow = 4 ;
%// calculate the end coordinates of the range
endRow = startRow + size(A,1)-1 ;
%// a bit more fiddling for the columns, as they could have many "characters"
cols = double(startCol)-64 ;
colIndex = sum(cols .* 26.^(length(cols)-1:-1:0)) ; %// index of starting column
endCol = eSheet1.Columns.Item(colIndex+size(A,2)-1).Address ;
endCol = endCol(2:strfind(endCol, ':')-1) ; %// column string reference
%// build range string in excel style
rngString = sprintf('=%s!$%s$%d:$%s$%d',sheetName,startCol,startRow,endCol,endRow) ;
myRange = eSheet1.Range(rngString) ; %// reference a range object
myRange.Value = A ; %// assign your values
%// Add the named range
e.Names.Add( 'TestRange' , myRange ) ;
测试数据故意从 Y
列溢出到 BD
列,以确保地址转换可以处理 (i) 添加一个字符,以及 (ii) 从 Ax
溢出] 到 By
。这似乎工作得很好,但如果你的数据数组不是那么宽,那么你也不必担心它。