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

  • Home
  • SEARCH
  • 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 848401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:56:12+00:00 2026-05-15T06:56:12+00:00

I wish to read files from a directory and iteratively perform an operation on

  • 0

I wish to read files from a directory and iteratively perform an operation on each file. This operation does not require altering the file.

I understand that I should use a for loop for this. Thus far I have tried:

FILES = ls('path\to\folder');

for i = 1:size(FILES, 1);
    STRU = pdbread(FILES{i});
end

The error returned here suggests to me, a novice, that listing a directory with ls() does not assign the contents to a data structure.

Secondly I tried creating a file containing on each line a path to a file, e.g.,

C:\Documents and Settings\My Documents\MATLAB\asd.pdb
C:\Documents and Settings\My Documents\MATLAB\asd.pdb

I then read this file using the following code:

fid = fopen('paths_to_files.txt');
FILES = textscan(fid, '%s');
FILES = FILES{1};
fclose(fid);

This code reads the file but creates a newline where a space exists in the pathway, i.e.

'C:\Documents'
'and'
'Setting\My'
'Documents\MATLAB\asd.pdb'

Ultimately, I then intended to use the for loop

for i = 1:size(FILES, 1)
    PDB = pdbread(char(FILES{i}));

to read each file but pdbread() throws an error proclaiming that the file is of incorrect format or does not exist.

Is this due to the newline separation of paths when the pathway file is read in?

Any help or suggestions greatly apppreciated.

Thanks,
S 🙂

  • 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-15T06:56:12+00:00Added an answer on May 15, 2026 at 6:56 am

    First Get a list of all files matching your criteria:
    ( in this case pdb files in C:\My Documents\MATLAB )

    matfiles = dir(fullfile('C:', 'My Documents', 'MATLAB', '*.pdb'))
    

    Then read in a file as follows:
    ( Here i can vary from 1 to the number of files )

    data = load(matfiles(i).name)
    

    Repeat this until you have read all your files.


    A simpler alternative if you can rename your files is as follows:-

    First save the reqd. files as 1.pdb, 2.pdb, 3.pdb,… etc.

    Then the code to read them iteratively in Matlab is as follows:

    for i = 1:n
        str = strcat('C:\My Documents\MATLAB', int2str(i),'.pdb'); 
        data = load(matfiles(i).name);
    
    % use our logic here  
    % before proceeding to the next file
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several XML files that I wish to read attributes from. My main
I have a simple bat script that copies files from an known directory to
I have a memory mapped file, from which I wish to parse the contents
I wish to perform an experiment many different times. After every trial, I am
I wish to migrate the database of a legacy web app from SQL Server
Thanks in advance for taking the time to read this. Apologies that it is
I have a MessageBox display when submitting a stream read file. However the box
i'm trying to read contents of PNG file. As you may know, all data
I wish to ship SQL server database file with my application. I am very
I've a csv file and I read the file as follows: #include <fstream> #include

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.