Loop variable names and file names

조회 수: 5 (최근 30일)
Karl
Karl 2013년 5월 13일
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

채택된 답변

Kye Taylor
Kye Taylor 2013년 5월 13일
편집: Kye Taylor 2013년 5월 13일
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.
  댓글 수: 1
Karl
Karl 2013년 5월 29일
That was it. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by