Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7069905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:30:09+00:00 2026-05-28T05:30:09+00:00

I’ve written the following function for importing excel files into matlab. The function works

  • 0

I’ve written the following function for importing excel files into matlab. The function works fine, where by inserting the path name of the files, the scripts imports them into the workspace. The function is shown below:

function Data = xls_function(pathName);

%Script imports the relevant .xls files into matlab - ensure that the .xls
%files are stored in a folder specified by 'pathName'.
%--------------------------------------------------------------------------
TopFolder = pathName; 
dirListing = dir(TopFolder);%Lists the folders in the directory specified
        %by pathName.
dirListing = dirListing(3:end);%Remove the first two structures as they
        %are only pointers.
for i = 1:length(dirListing);
    SubFolder{i} = dirListing(i,1).name;%obtain the name of each folder in
            %the specified path.
    SubFolderPath{i} = fullfile(pathName, dirListing(i,1).name);%obtain
            %the path name for each of the folders.
    ExcelFile{i} = dir(fullfile(SubFolderPath{i},'*.xls'));%find the 
            %number of .xls files in each of the SubFolders.
    for j = 1:length(ExcelFile{1,i});
        ExcelFileName{1,i}{j,1} = ExcelFile{1,i}(j,1).name;%find the name
                %of each .xls file in each of the SubFolders.
        for k = 1:length(ExcelFileName);
            for m = 1:length(ExcelFileName{1,k});
                [status{1,k}{m,1},sheets{1,k}{m,1},format{1,k}{m,1}]...
                    = xlsfinfo((fullfile(pathName,SubFolder{1,k},...
                    ExcelFileName{1,k}{m,1})));%gather information on the
                        %.xls files i.e. worksheet names.
                Name_worksheet{1,k}{m,1} = sheets{1,k}{m,1}{1,end};%obtain 
                        %the name of each of the .xls worksheets within 
                        %each spreadsheet.
            end
        end
    end
end

for n = 1:length(ExcelFileName);
    for o = 1:length(ExcelFileName{1,n});
            %require two loops as the number of excel spreadsheets varies 
            %from the number of worksheets in each spreadsheet. 
        TXT{1,n}{o,1} = xlsread(fullfile(pathName,SubFolder{1,n},...
            ExcelFileName{1,n}{o,1}),Name_worksheet{1,n}{o,1});%import the
                %relevant data from excel by using the spreadsheet and
                %worksheet names previously obtained. 
        Data.(SubFolder{n}){o,1} = TXT{1,n}{o,1};
    end
end

The only problem with the script is that it takes too long to run if the number of .xls files is large. I’ve read that vectorization would improve the running time, therefore I am asking for any advice on how I could alter this code to run faster, through vectorization.

I realise that reading a code like this isn’t easy (especially as my form of coding is by no means as efficient as I would like) but any advice provided would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:30:10+00:00Added an answer on May 28, 2026 at 5:30 am

    I don’t think vectorization applies to your problem – but one after the other.

    As an example for your data you could use cellfun to turn a loop vectorized:

    tmp = ExcelFileName{1,n}
    result_cell = cellfun(@(x) xlsread(fullfile(pathName,x)),tmp, 'UniformOutput', false))
    

    But the key problem is the poor implementation of xlsread and the other excel related functions in matlab. What they do is with every(!) function call they create a new excel process (which is hidden) in which they perform your command and then end it.

    I remember a tool at matlab central that reused the same excel instance and thus was very quick – but unfortunately I can no longer find it. But maybe you can find an example there on which you can base your own reader which reuses it.


    On a related note – Excel has the stupid limitation that it doesn’t allow you two files with the same name to be opened at the same time – and then fails with some error. So if you run your reading vectorized/parallel you are in for a whole new fun of strange errors 😀

    For myself I found the only propper way to deal with these documents through java with Apache POI libraries. These have the nice advantage you don’t need Excel installed – but unfortunatly require some programming.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.