Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

How do I simplify importing .xls files within a for loop?

Asked by Clifford Shelton on 13 Dec 2012

Thanks for any assistance in advance! I have the following problem: I have generated spreadsheets of data (.xls file for each baseball team).

I want to import one xls file, do some calculations, and then move on to the next team's xls file...so on and so on..

However, the way I designed my for loop, a window pops up where I have to manually select each individual .xls file I wish to be imported.

I would like to know: how can i automate this process? What code do I need so that the program itself will know exactly which xls file to import within the for loop without me needed to manually select it each time?.

Here is my code:

for Str = {'Diamondbacks' 'Braves' 'Orioles' 'Boston' 'Cubs' 'WhiteSox' 'Reds' 'Indians' 'Rockies' 'Tigers' 'Astros' 'Royals' 'Angels' 'Dodgers' 'Marlins' 'Brewers' 'Twins' 'Mets' 'Yankees' 'Athletics' 'Phillies' 'Pirates' 'Padres' 'Giants' 'Mariners' 'Cardinals' 'Rays' 'Rangers' 'BlueJays' 'Nationals'};
[fileToRead1, folder] = uigetfile();
AngelsData.xls = fullfile(folder, fileToRead1);
% Call the first function.
importhome(AngelsData.xls)
%IMPORTFILE(FILETOREAD1)
%  Imports data from the specified file
%  FILETOREAD1:  file to read
%  Auto-generated by MATLAB on 05-May-2012 23:12:52
% Import the file
sheetName='Sheet1';
[numbers, strings, raw] = xlsread(fileToRead1, sheetName);
if ~isempty(numbers)
    newData1.data =  numbers;
end
if ~isempty(strings) && ~isempty(numbers)
    [strRows, strCols] = size(strings);
    [numRows, numCols] = size(numbers);
    likelyRow = size(raw,1) - numRows;
    % Break the data up into a new structure with one field per column.
    if strCols == numCols && likelyRow > 0 && strRows >= likelyRow
        newData1.colheaders = strings(likelyRow, :);
    end
end

2 Comments

Walter Roberson on 13 Dec 2012

What pattern (if any) is there in the names of the files?

Clifford Shelton on 13 Dec 2012

the pattern is this: "teamname".xls For example: Braves.xls Angels.xls Tigers.xls so on and so forth.

Clifford Shelton

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson on 13 Dec 2012
Accepted answer

Replace

[fileToRead1, folder] = uigetfile();

with

folder = '';
fileToRead1 = [Str '.xls'];

Note: I would suggest that "AngelsData.xls" is a confusing name for a variable. It looks too much like a file name. It is a valid variable name, though, meaning "the field named 'xls' in the structure named 'AngelsData'"

3 Comments

Clifford Shelton on 13 Dec 2012

Thanks for the help! I'm getting a errorwhen I run the program. And I'm not sure what is going wrong...

I adjusted the coded like you said:

for Str = {'Diamondbacks' 'Braves' 'Orioles' 'Boston' 'Cubs' 'WhiteSox' 'Reds' 'Indians' 'Rockies' 'Tigers' 'Astros' 'Royals' 'Angels' 'Dodgers' 'Marlins' 'Brewers' 'Twins' 'Mets' 'Yankees' 'Athletics' 'Phillies' 'Pirates' 'Padres' 'Giants' 'Mariners' 'Cardinals' 'Rays' 'Rangers' 'BlueJays' 'Nationals'};
folder = '';
fileToRead1 = [Str '.xls'];
% Call the first function.
importhome(AngelsData.xls)
%IMPORTFILE(FILETOREAD1)
%  Imports data from the specified file
%  FILETOREAD1:  file to read
%  Auto-generated by MATLAB on 05-May-2012 23:12:52

I'm getting an error at the line of code that says:

importhome (AngelsData.xls)

When I first wrote this program the name of the fist file that i imported was called "AngelsData.xls". I first manually did an import and then generated the code for it through the code auto-generating function in Matlab.

Now I just want to have the whole process automated for all 30 teams (the importing and doing my calculations etc.).

How should I edit, or should I remove the line of code:

importhome (AngelsData.xls)

???? Thanks a bunch

Walter Roberson on 13 Dec 2012

I didn't say to remove the line

AngelsData.xls = fullfile(folder, fileToRead1);

Not that it is needed, really. What should be done is to remove it and to use

importhome(filetoRead1);
Clifford Shelton on 13 Dec 2012

I tried that and still got this error:

??? Error using ==> xlsread at 122 Filename must be a string.

Error in ==> importhome at 10 [numbers, strings, raw] = xlsread(fileToRead1, sheetName);

Error in ==> baseball2 at 9 importhome(fileToRead1);

Stil stumped :-(

Walter Roberson

Contact us