2014-01-08 51 views
2

我正在使用xlswrite將數據從Matlab寫入Excel。我想命名數據寫入的範圍。我搜索了這個搜索幫助文件,並找不到關於此的任何信息。有Matlab腳本來訪問範圍名稱,但沒有實際在Matlab中爲Excel範圍創建名稱。有沒有人有任何提示或洞察到這個問題?使用Matlab在Excel中創建命名範圍

+0

你說的'名字的意思是range' - 只想寫爲'A2:D6'在標記片'MyData',或你的意思是根據其他輸入自動創建字符串「A2:D6」嗎?舉例說明你想要做什麼。 – nkjt

+0

我想在標有'MyData'的表格中寫入'A2',並將範圍命名爲'Number_of_Widgets'(請參閱Excel下的Excel範圍名稱 - 公式 - >名稱管理器)。我有Word VBA代碼訪問Excel範圍名稱並將它們匹配到Word中同名的書籤。 – user1854628

+0

我不相信'xlswrite'可以實現。如果您有權訪問它,也許可以查看「MLGetMatrix」。 – nkjt

回答

2

這個MATLAB代碼解決了這個問題。它採取了一些挖掘和實驗,所以我想我會分享:

function NameRange(r2,c2,b2,RangeName,SummaryFileName,SheetName) 
% -------------------- 
% Function: NameRange 
% Description: 
% This script names a specific range in Excel 
% 
% input: 
% r2 = rows in data being written to Excel i.e. size(matrix, 2) 
% c2 = columns in data being written to Excel i.e. size(matrix,1) 
% b2 = the starting cell you are writing to i.e. whatever cell you put into xlswrite i.e. 'A2' 
% Rangename = What you want to name the range 
% SummaryFileName is the address to the file, '**FILENAME**.xls' 
% Sheet Name is the worksheet name i.e. 'Doc Details' 
% 
% Example: NameRange(5,6,'A3','Tester1234','**FILENAME**.xls','Doc Details') 
% 
% Output - named range 
% If you named a sheet incorrectly, this is one of the Possible Errors 
% Description: The name that you entered is not valid. 
% 
% Reasons for this can include: 
% -The name does not begin with a letter or an underscore 
% -The name contains a space or other invalid characters 
% -The name conflicts with an Excel built-in name or the 
%  name of another object in the workbook 
% Help File: xlmain11.chm 
% Help Context ID: 0 
% ---------------------- 

% Create object. 
ExcelApp = actxserver('Excel.Application'); 
% Show window (optional). 
ExcelApp.Visible = 1; 
% Open file located in the current folder. 
ExcelApp.Workbooks.Open(SummaryFileName); 
% create handle for parsing sheets 
Sheets = ExcelApp.ActiveWorkBook.Sheets; 
% grab the specified sheet 
cursheet = get(Sheets, 'Item', SheetName); 
% calculate start and end 
[cr1, cr2]=nn2an2(r2,c2,b2); 
A1Notation = [cr1 ':' cr2]; 
cursheet.Range(A1Notation).Name = RangeName; 

% Pull the shortname from the path to close the file 
[pathstr, name, ext] = fileparts(SummaryFileName); 
ShortName = [name ext]; 

Excel = actxGetRunningServer('Excel.Application'); 
Excel.WorkBooks.Item(ShortName).Save; 
Excel.WorkBooks.Item(ShortName).Close; 
Excel.Quit; 

function [cr1 ,cr2] = nn2an2(r2, c2, b2) 
% r1 and c1 is the size of the data you are writing to matlab, and b2 is 
% the starting cell 

% convert alpha, number format to number, number format 
% this is the starting cell 
t = find(isletter(b2)); 
t2 = abs(upper(b2(t))) - 64; 
if(length(t2) == 2), t2(1) = t2(1) * 26; end 
c1 = sum(t2); r1 = str2num(b2(max(t) + 1:length(b2))); 

% find the other corner of the matrix in A1 notation by adding the two 
% together 
c3 = c1+c2-1; 
r3 = r1+r2-1; 

% convert number, number format to alpha, number format 
t3 = [floor((c3 - 1)/26) + 64 rem(c3 - 1, 26) + 65]; 
if(t3(1)<65), t3(1) = []; end 
% other corner found 
cr2 = [char(t3) num2str(r3)]; 
cr1 = b2; 
end 

end