将matlab求出的矩阵保存在Excel表格中
(2012-04-18 20:29:33)
标签:
matlab矩阵excel表格教育 |
分类: Matlab与VC混编 |
将matlab求出的矩阵保存在Excel表格中
转贴Genial分享程序
function xlswrite(m,header,colnames,filename);
%
xlswrite
%
%
%
%
%
%Inputs:
%
% (Optional):
%
%
%
%
%
%
%
%
% ex:
%
%
%
%
%
%
%
%
%
%
%
%
% Scott Hirsch
% The MathWorks
% This is provided free, no warranty, ...
% Copied from ActiveX example in documentation
[nr,nc] = size(m);
if nc>256
end;
% Open Excel, add workbook, change active worksheet,
% get/put array, save.
% First, open an Excel Server.
Excel = actxserver('Excel.Application');
%If the user does not specify a filename, we'll make Excel
visible
%If they do, we'll just save the file and quit Excel without ever
making
% it visible
if nargin<4
end;
% Insert a new workbook.
Workbooks = Excel.Workbooks;
Workbook = invoke(Workbooks, 'Add');
% Make the first sheet active.
Sheets = Excel.ActiveWorkBook.Sheets;
sheet1 = get(Sheets, 'Item', 1);
invoke(sheet1, 'Activate');
% Get a handle to the active sheet.
Activesheet = Excel.Activesheet;
%Write header
if nargin<2 | isempty(header)
elseif iscell(header)
else
end;
�d column names
if nargin>2 &
~isempty(colnames)
end;
% Put a MATLAB array into Excel.
FirstRow =
nhr+1;
LastRow = FirstRow+nr-1;
FirstCol =
'A';
LastCol = localComputLastCol(FirstCol,nc);
ActivesheetRange = get(Activesheet,'Range',...
[FirstCol num2str(FirstRow)],[LastCol num2str(LastRow)]);
set(ActivesheetRange, 'Value', m);
% If user specified a filename, save the file and quit
Excel
if nargin==4
end;
�lete the ActiveX object
delete(Excel)
function LastCol = localComputLastCol(FirstCol,nc);
% Comput the name of the last column where we will place data
%Input
%
%
%Excel's columns are named:
% A B C ... A AA AB AC AD .... BA BB BC ...
FirstColOffset = double(FirstCol) -
double('A');
if
nc<=26-FirstColOffset
else
A
end;
使用方法:
>> m = rand(100,4);
>> header = 'my data';
>> colnames =
{'Ch1','Ch2','Ch3','Ch4'};
>>
Excel file myfile.xls has been created.
来源:http://liuxinhust.blog.163.com/blog/static/657896120072275564194/

加载中…