matlab txt写入excel,改编了一个将 text 文本写入 excel 里的 matlab 函数

本文介绍了一个Matlab函数,用于将文本数据(包括标题和列名)导入Excel。函数已更新,可以读取纯文本并允许在任意路径下读写Excel文件。函数使用`textread`读取数据,然后利用ActiveX接口写入Excel,支持自定义工作表名。
摘要由CSDN通过智能技术生成

今天花了大半天的时间修改了一个将 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)]);

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值