今天花了大半天的时间修改了一个将 text 文本写入 excel 里的 matlab
函数,原来不能读纯文本该后可以读了.数据和文本可以一块读进 excel 里,对数据处理非常方便.
另外,把原来只能读matlab当前路径,该为了可以读任何路径,并且能保存到任何路径里去.还有其他方面,不便细说,有兴趣的话,看看便知晓.
原来的程序为:
function dat2xls(varargin)
�T2XLS Copying (ASCII) data file (including headers
& column names) to Excel.
%
% dat2xls : This program reads
a (text data file) consisting of
% headers, column titles and column data (see attached
% data.txt). Then it opens or creates existing
excel
% file and saves data into specified sheetname.
%
% NOTE: The program will
automatically identify the number of
% headerlines, columns and the column names using what so
called
% "common sense"!
%
% dat2xls(datfile,xlsfile,sheetname)
% dat2xls(datfile,xlsfile)
%
% datfile: Name of data file.
% xlsfile: Name of excel file.
% sheetname: sheet name (optional, default is 'Sheet1')
% if specified, a sheet with the specified name must
% be existing.
%
% Example:
%
% datfile = 'data.txt'; % xlsfile = 'data.xls';
% sheetname = 'Sheet2';
% dat2xls(datfile,xlsfile,sheetname)
% �t2xls(datfile,xlsfile) % Will write to 'Sheet1'
%
% Copyright 2004 Fahad Al
Mahmood
% Version: 1.0
$ $Date: 18-Feb-2004
% 1.5 $ $Date: 3-Mar-2004 % Fixed header bug + saving bug
%
% See also XLSWRITE, XLSHEETS
if nargin==3
datfile =
varargin{1};
xlsfile =
varargin{2};
sheetname =
varargin{3};
elseif nargin==2
datfile =
varargin{1};
xlsfile =
varargin{2};
sheetname =
1;
end
Excel = actxserver('Excel.Application');
%set(Excel, 'Visible', 1);
% Creating a new excel workbook or Opening (xlsfile)
if exist(xlsfile,'file')==0
% The
following case if excel file does not exist (Creating New
File)
Workbook =
invoke(Excel.Workbooks,'Add');
new=1;
else
% The
following case if file does exist (Opening File)
disp(['Opening Excel File ...(' xlsfile ')']);
Workbook =
invoke(Excel.Workbooks, 'open', [pwd filesep xlsfile]);
new=0;
end
[data] =
textread(datfile,'%s','delimiter','\n','emptyvalue',NaN);
k=1;m=[];
for i=1:length(data)
if
~isempty(str2num(data{i}))
m(k,:) = str2num(data{i});
k=k+1;
elseif
~isempty(data{i})
header{i} = data{i};
end
end
% 遇到空行停下.
for ii=length(header):-1:1
if
~isempty(header{ii})
col_titles = header{ii};
break;
end
end
% 将串col_titles中字符放到 数组colnames{c}中
c=1;
rem = col_titles;
while length(rem)~=0
[word,rem]=strtok(rem); %word为取rem第一个字符
if
~isspace(word)
colnames{c} = word;
c=c+1;
end
end
for i=1:length(header)-1
H{i}=header{i};
end
header=[];
if exist('H')
header =
H;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modified copy of (xlswrite)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[nr,nc] = size(m);
if nc>256
error('Matrix is too large. Excel only supports
256 columns');
end;
% Make the sheet active.
Sheets = Excel.ActiveWorkBook.Sheets;
target_sheet = get(Sheets, 'Item', sheetname);
invoke(target_sheet, 'Activate');
% Get a handle to the active sheet.
Activesheet = Excel.Activesheet;
%Write header
if isempty(header)
nhr=0;
elseif iscell(header)
nhr =
length(header); %Number header rows
for
ii=1:nhr
ActivesheetRange = get(Activesheet,'Range',['A' num2str(ii)],['A'
num2str(ii)]);