Loop variable names and file names
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have asked this questions several times before, http://www.mathworks.se/matlabcentral/answers/74778-loop-file-and-variablenames-when-importing
and
A lot of the material is just copied from the old posts.
In the following, I am trying to make make variable names ending with "2011", "2012" and imporing data to these variables from files with names "2011", "2012" etc. It doesn'n seem to work. Below the loop I specify what happends.
Name = {'2011', '2012', '2013_1', '2014_1', '2014_1s2', '2014_1s5'};
n = length(Name);
H = struct('Data', cell(1, n), 'Name', cell(1, n));
G = struct('Data', cell(1, n), 'Name', cell(1, n));
for iName = 1:n
aName = Name{iName};
[~, ~, raw] = xlsread(sprintf('Q:\\Karl\\HH\\FUT2013\\LOTTE\\%s.xlsx', aName),'B8:I18');
H(i).Name = aName;
H(i).Data = reshape([raw{:}],size(raw));
[~, ~, raw] = xlsread(sprintf('Q:\\Karl\\HH\\FUT2013\\LOTTE\\%s.xlsx', aName),'B28:I38');
G(i).Name = aName;
G(i).Data = reshape([raw{:}],size(raw));
end
I got the error message: "Cell contents indices must be greater than 0".
That was the complete erroe message. If I run the commands line by line, there is no error. The error appears a couple of seconds after the "end" command.
My workspace contains the following variables:
G, which is a 1x6 struct where each element equals "<1x1 struct>"
H, which is the same as G above
Name, 1x6 cell: '2011' '2012' '2013_1' '2014_1' '2014_1s2' '2014_1s5'
aName, which is a 1x1 with value 2011
iName, which equals 1
n, which equals 6
raw, which is a 11x8 cell containg the data from the excelfile "2011" that should have the variable name "H2011".
So the G and H variables were made, but they did not have any content. It looks like the commands not inclunding G or H work. Maybe that fact that the matrices I import from excel is 11x8 matrices and that H and G is made as 1xn cells in the first lines of commands can explain why your commands doesn't work
0 commentaires
Réponse acceptée
Kye Taylor
le 13 Mai 2013
Modifié(e) : Kye Taylor
le 13 Mai 2013
Check your for-loop. The iteration variable is iName, but you use i in the loop's body. Since you do not define i, MATLAB uses the imaginary unit. You cannot index into an array with an imaginary number, hence the error. As a first attempt to fix it, try replacing i with iName.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!