how to read a .xls file in the matlab ?

7 views (last 30 days)
% loadModelXls.m - LOADS THE MODEL STRUCTURE AND PARAMETERS INTO MATLAB FROM AN EXCEL FILE
%
function [] = loadModelXls(idR)
global R;
warning off
% idR is the identifier of the reactor when more than one is simulated.
% The reactor no. 1 used (idR=1) requires no number in the xls sheets names.
% The second and following reactors, require the sheets names in the excel followed,...
% ... by the idR number at the end to indicate to which reactor they refer. e.g. StateVar2
if idR==1,id ==1;else id ==0;end
% VARIABLES AND PARAMETERS NAMES AND VALUES ARE READ FROM THE EXCEL FILE.
[OperatParam, pOpNames] = xlsread(strcat('MyParameters.xls'), strcat('OperatParam', id));
[StateVar, StNames] = xlsread(strcat('MyParameters.xls'), strcat('StateVar', id));
[AlgebraicParam,pAlgNames]= xlsread(strcat('MyParameters.xls'), strcat('AlgebraicParam', id));
[KineticParam, pKtNames] = xlsread(strcat('MyParameters.xls'), strcat('KineticParam', id));
[StoichMatrix, RateNames]= xlsread(strcat('MyParameters.xls'), strcat('StoichMatrix', id));
[FeedProgram, FdNames] = xlsread(strcat('MyParameters.xls'), strcat('FeedProgram', id));
FdNames = FdNames(2:size(FdNames,2));
RateNames = RateNames(1,2:size(RateNames,2));
% MATLAB STRUCTURE TYPE VARIABLES ARE CREATED FOR GROUP OF VARIABLES AND PARAMETERS WITH ...
% ...THE NAMES AND VALUES GIVEN IN THE EXCEL FILE AND FOR EACH REACTOR CONSIDERED.
%
% NOTE HOW SOME VARIABLES ARE ALSO COPIED INTO VECTORS TO FACILITATE
% MATRIX AND VECTOR OPERATIONS LATER. THAT IS THE CASE OF StV, AlgStV, rV, etc.
aux = '';
% Structure with the OPERATIONAL PARAMETERS named "pOp".
for i=1:length(OperatParam),
% Building the string command for the structure, last without ', '.
if i<length(OperatParam),
aux = strcat(aux, char(39), pOpNames(i), char(39), ', ',num2str(OperatParam(i)),', ');
else
aux = strcat(aux, char(39), pOpNames(i), char(39), ', ',num2str(OperatParam(i)));
end
end
aux = char(aux);
eval(strcat('pOp = struct(', aux, ');'));
pOp.pOpNames = pOpNames;
aux = '';
% Structure with the STATE VARIABLES AND THEIR INITIAL VALUES named "St".
for i=1:length(StateVar),
% Building the string command for the structure, last without ', '.
if i<length(StateVar),
aux = strcat(aux, char(39), StNames(i), char(39), ', ',num2str(StateVar(i)),', ');
else
aux = strcat(aux, char(39), StNames(i), char(39), ', ',num2str(StateVar(i)));
end
end
aux = char(aux);
eval(strcat('St = struct(', aux, ');'));
St.Phase = StNames(:,4);
St.StNames = StNames;
StV = StateVar;
aux = '';
% Structure with the ALGEBRAIC PARAMETERS named "needed for calcualtion of the algebra.
for i=1:length(AlgebraicParam),
% Building the string command for the structure, last without ', '.
if i<length(AlgebraicParam),
aux = strcat(aux, char(39), pAlgNames(i), char(39), ', ',num2str(AlgebraicParam(i)),', ');
else
aux = strcat(aux, char(39), pAlgNames(i), char(39), ', ',num2str(AlgebraicParam(i)));
end
end
aux = char(aux);
eval(strcat('pAlg = struct(', aux, ');'));
pAlg.pAlgNames = pAlgNames;
aux = '';
% Structure with the KINETIC PARAMETERS.
for i=1:length(KineticParam),
% Building the string command for the structure, last without ', '.
if i<length(KineticParam),
aux = strcat(aux, char(39), pKtNames(i), char(39), ', ',num2str(KineticParam(i)),', ');
else
aux = strcat(aux, char(39), pKtNames(i), char(39), ', ',num2str(KineticParam(i)));
end
end
aux = char(aux);
eval(strcat('pKt = struct(', aux, ');'));
pKt.pKtNames = pKtNames;
% STOICHIOMETRY MATRIX.
stoM = StoichMatrix;
% Structure with the FEEDING PARAMETERS.
aux = '';
for i=1:length(FdNames),
% Building the string command for the structure, last without ', '.
if i<length(FdNames),
aux = strcat(aux, char(39), FdNames(i), char(39), ', FeedProgram(:,', num2str(i+1), '), ');
else
aux = strcat(aux, char(39), FdNames(i), char(39), ', FeedProgram(:,', num2str(i+1), ')');
end
end
aux = char(aux);
eval(strcat('pFd = struct(', aux, ');'));
pFd.Time = FeedProgram(:,1);
pFd.FdNames = FdNames;
% Structure with the EFFLUENT COMPOSITION IS ALSO CREATED.
aux = '';
for i=1:length(StateVar),
% Building the string command for the structure, last without ', '.
if i<length(StateVar),
aux = strcat(aux, char(39), StNames(i), char(39), ', ',num2str(StateVar(i)),', ');
else
aux = strcat(aux, char(39), StNames(i), char(39), ', ',num2str(StateVar(i)));
end
end
aux = char(aux);
eval(strcat('Eff = struct(', aux, ');'));
Eff.Phase = StNames(:,4);
Eff.EffNames = [{'Qgas'}; {'Q'}; StNames(:,1)];
Eff.Qgas = 0; Eff.Q = 0;
for i=1:length(Eff.EffNames),
Eff.EffNames(i) = strcat(Eff.EffNames(i),'_out');
end
% ALL THE STRUCTURES ARE EMBEDDED INTO THE GLOBAL STRUCTURE R.
R(idR).pOp=pOp;
R(idR).St=St;
R(idR).StV=StV;
R(idR).pAlg=pAlg;
R(idR).pKt=pKt;
R(idR).pFd=pFd;
R(idR).Eff=Eff;
R(idR).stoM=stoM;
% % Structure with the parameter values.
% prm.FeedProgram = FeedProgram;
% prm.OperatParam = OperatParam;
% prm.StateParam = StateParam;
% prm.AlgebraicParam = AlgebraicParam;
% prm.KineticParam = KineticParam;
% prm.StoichMatrix = StoichMatrix;
  1 Comment
Stephen23
Stephen23 on 15 Sep 2015
Edited: Stephen23 on 15 Sep 2015
This code uses eval everywhere to perform simple allocation to variables. This is a very poor programming practice that should be avoided, even tough beginners seem to love using eval everywhere.
Many of the variables are structures anyway, which makes the buggy and slow eval statements completely superfluous as they could be simply replaced with dynamic fieldnames, or simple variable allocations.
Read this to know why writing code with eval is a bad practice:
The code could be significantly improved, such as the cell arrays are not indexed correctly but are converted to strings using char, the enlarging of aux inside every for-loop, the generation of structures using string evaluation, multiple conversion between numeric and string representation of numeric values. All of these are slow, buggy and inefficient.
Upload an sample excel file and we can show how this can be simplified.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 14 Sep 2015
That code already does read in several worksheets with lines of code like
[OperatParam, pOpNames] = xlsread(strcat('MyParameters.xls'), strcat('OperatParam', id));
which is the same as
[OperatParam, pOpNames] = xlsread('MyParameters.xls', ['OperatParam', id]);
so, what is your question?
  2 Comments
Engr. Hassan  Hameed
Engr. Hassan Hameed on 15 Sep 2015
I know tat these lines will read the .xls file. But the main problem is that these commands are not reading the file.
Image Analyst
Image Analyst on 15 Sep 2015
That sounds contradictory - they read it but they don't read it. Do you mean that the lines of code SHOULD read the .xls workbook file but do not because of some kind of error? If so, what is the error? Copy and paste all the red text. Do you have a license for the full version of Excel (not the starter version)? Or does read it, but in the code after that does things with the data that you don't like?

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!