Loop file- and variablenames when importing

I want to make a loop that loops over the number 2011 in the following script.
[~, ~, raw] = xlsread('adress\*2011*.xlsx','B8:I18'); *H2011 *= reshape([raw{:}],size(raw));
[~, ~, raw] = xlsread('adress\*2011*.xlsx','B28:I38'); *G2011 *= reshape([raw{:}],size(raw));
As you can see, "2011" is the filename, but also used in the matrix definitions (H2011, G2011). Also, I call on a specific cell range from the excelfiles (B8:I18, b28:I38 etc).
I have several excelfiles with names "2011", "2012", "2013_1", "2014_1", "2014_1s2" and "2014_1s5". Instead of repeating the excact same commands as aboove by changing "2011" in the filaname and matrix name for all the names mentioned, I wish to make a loop that does this. Does anybody have suggestions?

답변 (1개)

Jan
Jan 2013년 5월 6일

0 개 추천

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('adress\%s.xlsx', aName),'B8:I18');
H(i).Name = aName;
H(i).Data = reshape([raw{:}],size(raw));
[~, ~, raw] = xlsread('adress\*2011*.xlsx','B28:I38');
G(i).Name = aName;
G(i).Data = reshape([raw{:}],size(raw));
end
Do not hide important information in the name of a variable. This would increase the complexity of the methods, required to operate on the data. It is trivial to expand the above method to further 1000 files, while hard coding the names like "G2011" will lead to troubles soon. Using EVAL to create such names dynamically is a bad idea also: 1. it slows down Matlab substantially, 2. it makes debugging so much harder that the program explodes up to a certain level of complexity.

댓글 수: 4

Karl
Karl 2013년 5월 6일
편집: Karl 2013년 5월 6일
I got the error message: "Cell contents indices must be greater than 0".
I ran the folowing commands:
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('Q:\\Karl\\HH\\FUT2013\\LOTTE\\2011.xlsx','B28:I38');
G(i).Name = aName;
G(i).Data = reshape([raw{:}],size(raw));
end
No variables with name "H" og "G" were made. Only one variable "raw" which contains what is supposed to be H2011 is made...
Jan
Jan 2013년 5월 6일
편집: Jan 2013년 5월 6일
Please post the complete error message and show us the line, which causes the error.
What does this exactly mean:
No variables with name "H" og "G" were made
We can see the lines, where at least the empty structs are created. When you cannot find the variables H and G, there is another problem.
Karl
Karl 2013년 5월 7일
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".
Karl
Karl 2013년 5월 7일
Addition: 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?

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2013년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by