MATLAB对excel文件读写操作

@MATLAB对excel文件读写操作

uigetfile函数的使用

通过uigetfile函数选择数据文件,并将文件名和文件的绝对路径存储在file结构体。

[file.name,file.path]=uigetfile('.csv','选择CSV数据文件')

对于批量读取同类型文件,可以使用uigetdir函数:

selpath=uigetdir;
files=dir(selpath);
files=files(cell2mat({files(:).isdir})~=1);
for i=1:length(files)
	if files(i).name(end-2:end)=='csv'
	fprintf(files(i).name);
	end
end

importdata函数读取

dataname=strcat(file.path,file.name);
data=importdata(dataname);

data结构体包含以下三部分:
data: double类型数据
textdata: cell类型的所有文本数据
colheaders: cell类型的数据头

excel数据写入xlswrite函数

为了批量写入多行数据,我将数组变量和文本变量保存在celll类型中。
xlswrite函数用法及参数如下:
xlswrite(filename,A,sheet,xlRange) 将数据A写入filename文件指定的sheet工作表的xlRange位置。

elcelname='统计分析.xlsx';
title={'类型''平均值''最小值','有效值','方差'};
GroupA_cell=['Group A',num2cell(data_A)];
GroupB_cell=['Group B',num2cell(data_B)];
datawrite=[title;GroupA_cell;GroupB_cell];
filename={filename(1:end-4)}; %去除文件后缀.csv,并将字符串转化为cell

将数据写入excel文件中

if exist(excelname,'file')==2
	[~,txt,~]=xlsread(excelname,'sheet1');
	[txtlen,~]=size(txt);
	Alocat=strcat('A',num2str(txtlen+2));
	xlswrite(excelname,filename,'sheet1',Alocat);
	Alocat=strcat('A',num2str(txtlen+3));
	xlswrite(excelname,datawrite,'sheet1',Alocat);
else
	xlswrite(excelname,filename,'sheet1','A1');
	xlswrite(excelname,datawrite,'sheet1','A2');
end

至此,批量写入数据完成。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值