欢迎光临散文网 会员登陆 & 注册

2020-11-03 MATLAB App Designer—读取Excel信息

2020-11-03 10:34 作者:-何其-  | 我要投稿

背景

在excel文件里存放有数据,目前需要读取该.xlsx内的多个工作簿所包含的数据,并将读取的数据设置成table格式

要用到的函数

sheetnames(ProjectPath); % 读取Excel文件的工作簿名称 % 为 str 形式

 readtable(ProjectPath,'ReadVariableNames',true,'Sheet',SheetName);

% ProjectPath % 文件绝对路径

% SheetName % 需要读取的工作簿名称

函数使用

% 首先获取excel文件内的工作簿名称 % ExcelBookList_Excel

ExcelBookList_Excel = sheetnames(app.inData.ProjectPath); % 读取Excel文件的工作簿名称 % 为 str 形式

% 如果存在以下工作簿就读取该工作簿的数据

ExcelBookList = {'ColNameList';'LimintValueData';'InitialValue';'CacluateSymData'; % 为cell 形式 

                            'IntervalcolName';'PreTreatMentPlan';'PreWorkflow';'Info';

                            'Template';'MainFileData';'SensorsData';'PathData'};

% 对每个工作簿采用for循环单独读取

for ii =  1 : length( ExcelBookList )

         try eval( strcat( ExcelBookList{ ii } = readtable( ProjectPath,' , 39 , 'ReadVariableNames' , 39 , ', true,' , 39 , 'Sheet' , 39 , ',' , 39 , ExcelBookList{ ii }  , 39 , ');'  ) );

          catch; disp(strcat('ERROR!!!读取失败:',32,ExcelBookList{ ii } ));

           end

end

进阶加速

采用for循环读取很耗时间,想了个办法加速读取数据,使用下面的函数,数据保存在inData中,为struct结构形式。

实测该方案能节约for循环下的一半左右的时间。

cell2mat(cellfun( @(x) xifun_readtable(  ProjectPath , x ),ExcelBookList,'UniformOutput',false))

function [ Message ] = xifun_readtable( ProjectPath ,x ) % 读取工程文件指定工作簿

                try eval(strcat('inData.',x,'=readtable(ProjectPath,',...

                        39,'ReadVariableNames',39,',true,',39,'Sheet',39,',',39,x,39,');' ));

                Message = 1;

                catch;Message = 0;  disp(strcat('ERROR!!!',32,x));

                end

end


2020-11-03 MATLAB App Designer—读取Excel信息的评论 (共 条)

分享到微博请遵守国家法律