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

Thread Subject:
Importing Text Data To Matrix

Subject: Importing Text Data To Matrix

From: D Nguyen

Date: 29 Jul, 2009 16:40:20

Message: 1 of 12

Hi all,

I am currently working and need to learn Matlab for it. I learned a bit about using matrices/vectors and of the loops, and have gotten a slight feel for it. Right now I am trying to figure out how to import in a txt file of unknown rows into a matrix. The text file contains both strings and numeric data, some lines have both numbers and alphanumeric characters such as a directory path, or a file name. Not all lines within the txt file start with a character, it could be a space or even multiple spaces. My task is to import all these lines into a one column matrix with the same amount of rows as the text file minus the blank lines. At first I tried using the load('file') function but it seems the spaces on some of the lines are intepreted as columns. I am rather unsure of how textscan works. What would be the most simplest way to achieve what I want to do? Thank you for your help!

Subject: Importing Text Data To Matrix

From: Dave Brackett

Date: 29 Jul, 2009 20:24:01

Message: 2 of 12

"D Nguyen" <dnguyen1022@gmail.com> wrote in message <h4pu1k$581$1@fred.mathworks.com>...
> Hi all,
>
> I am currently working and need to learn Matlab for it. I learned a bit about using matrices/vectors and of the loops, and have gotten a slight feel for it. Right now I am trying to figure out how to import in a txt file of unknown rows into a matrix. The text file contains both strings and numeric data, some lines have both numbers and alphanumeric characters such as a directory path, or a file name. Not all lines within the txt file start with a character, it could be a space or even multiple spaces. My task is to import all these lines into a one column matrix with the same amount of rows as the text file minus the blank lines. At first I tried using the load('file') function but it seems the spaces on some of the lines are intepreted as columns. I am rather unsure of how textscan works. What would be the most simplest way to achieve what I want to do? Thank you for your
help!



textscan would be easiest. firstly you would need to open the file for reading e.g.:
fid =fopen('textfile.txt','r') % fid is the file identifier

Then to read the lines you could use either:
a=textscan(fid,'%s','delimiter', '','whitespace','') % would read in each line as a string ignoring blank lines but retaining leading and trailing whitespace in each line

a=textscan(fid,'%s','delimiter', '') % would ignore blank lines and also ignore leading and trailing whitespace

a=textscan(fid,'%s','delimiter', '\n') % would retain blank lines and ignore leading and trailing whitespace

These would output a single column cell array which can be referenced using curly brackets i.e. a{1}.

Finally, following the textscan line you would need to close the file e.g.:
fclose(fid)

Hope that helps.

Subject: Importing Text Data To Matrix

From: D Nguyen

Date: 30 Jul, 2009 04:43:01

Message: 3 of 12

"Dave Brackett" <davebrackett@hotmail.com> wrote in message <h4qb51$h0v$1@fred.mathworks.com>...
> "D Nguyen" <dnguyen1022@gmail.com> wrote in message <h4pu1k$581$1@fred.mathworks.com>...
> > Hi all,
> >
> > I am currently working and need to learn Matlab for it. I learned a bit about using matrices/vectors and of the loops, and have gotten a slight feel for it. Right now I am trying to figure out how to import in a txt file of unknown rows into a matrix. The text file contains both strings and numeric data, some lines have both numbers and alphanumeric characters such as a directory path, or a file name. Not all lines within the txt file start with a character, it could be a space or even multiple spaces. My task is to import all these lines into a one column matrix with the same amount of rows as the text file minus the blank lines. At first I tried using the load('file') function but it seems the spaces on some of the lines are intepreted as columns. I am rather unsure of how textscan works. What would be the most simplest way to achieve what I want to do? Thank you for your
> help!
>
>
>
> textscan would be easiest. firstly you would need to open the file for reading e.g.:
> fid =fopen('textfile.txt','r') % fid is the file identifier
>
> Then to read the lines you could use either:
> a=textscan(fid,'%s','delimiter', '','whitespace','') % would read in each line as a string ignoring blank lines but retaining leading and trailing whitespace in each line
>
> a=textscan(fid,'%s','delimiter', '') % would ignore blank lines and also ignore leading and trailing whitespace
>
> a=textscan(fid,'%s','delimiter', '\n') % would retain blank lines and ignore leading and trailing whitespace
>
> These would output a single column cell array which can be referenced using curly brackets i.e. a{1}.
>
> Finally, following the textscan line you would need to close the file e.g.:
> fclose(fid)
>
> Hope that helps.

Thank you for your reply, however I don't simply want to copy and paste. I wish to understand this function a bit better.
What does the 'r' do in the fopen function?
What about the (..,'delimiter', '', 'whitespace', '') for the textscan function?
Also if I wanted to import mutiple text file of the same format, except have file 1 in column 1, file 2 in column 2, how would I go about doing that? Thanks again!

Subject: Importing Text Data To Matrix

From: Matt Fig

Date: 30 Jul, 2009 04:52:01

Message: 4 of 12

"D Nguyen" <dnguyen1022@gmail.com> wrote in message
> Thank you for your reply, however I don't simply want to copy and paste. I wish to understand this function a bit better.
> What does the 'r' do in the fopen function?
> What about the (..,'delimiter', '', 'whitespace', '') for the textscan function?
> Also if I wanted to import mutiple text file of the same format, except have file 1 in column 1, file 2 in column 2, how would I go about doing that? Thanks again!

If you wish to understand what those functions do, and the arguments they take, look to the help and documentation.

>>help textscan
>>doc textscan
>>help fopen
>>doc fopen

Subject: Importing Text Data To Matrix

From: Dave Brackett

Date: 30 Jul, 2009 10:48:01

Message: 5 of 12

"Matt Fig" <spamanon@yahoo.com> wrote in message <h4r8th$pss$1@fred.mathworks.com>...
> "D Nguyen" <dnguyen1022@gmail.com> wrote in message
> > Thank you for your reply, however I don't simply want to copy and paste. I wish to understand this function a bit better.
> > What does the 'r' do in the fopen function?
> > What about the (..,'delimiter', '', 'whitespace', '') for the textscan function?
> > Also if I wanted to import mutiple text file of the same format, except have file 1 in column 1, file 2 in column 2, how would I go about doing that? Thanks again!
>
> If you wish to understand what those functions do, and the arguments they take, look to the help and documentation.
>
> >>help textscan
> >>doc textscan
> >>help fopen
> >>doc fopen


While the textscan documentation can be daunting as it is a very flexible and powerful function don't be put off. There are tables on that page with different parameters that can be set to achieve what you want, e.g. which delimiter you want, and how you want it to handle whitespace.

The 'r' in fopen simply means that you want to open that file for reading. If it was a 'w' it would open it for writing. Again, there is a list of these arguments in the documentation.

Regarding your additional question about how to handle multiple files you can do it like this:

a{1}=textscan(fid,...)
a{2}=textscan(fid2,...)
etc.

If you have many files you many wish to put this into a loop e.g.

for i=1:number_of_files
fid=fopen('file.txt','r');
a{i}=textscan(fid,...);
fclose(fid);
end

Subject: Importing Text Data To Matrix

From: D Nguyen

Date: 4 Aug, 2009 16:24:01

Message: 6 of 12

"Dave Brackett" <davebrackett@hotmail.com> wrote in message <h4rtp1$f37$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <h4r8th$pss$1@fred.mathworks.com>...
> > "D Nguyen" <dnguyen1022@gmail.com> wrote in message
> > > Thank you for your reply, however I don't simply want to copy and paste. I wish to understand this function a bit better.
> > > What does the 'r' do in the fopen function?
> > > What about the (..,'delimiter', '', 'whitespace', '') for the textscan function?
> > > Also if I wanted to import mutiple text file of the same format, except have file 1 in column 1, file 2 in column 2, how would I go about doing that? Thanks again!
> >
> > If you wish to understand what those functions do, and the arguments they take, look to the help and documentation.
> >
> > >>help textscan
> > >>doc textscan
> > >>help fopen
> > >>doc fopen
>
>
> While the textscan documentation can be daunting as it is a very flexible and powerful function don't be put off. There are tables on that page with different parameters that can be set to achieve what you want, e.g. which delimiter you want, and how you want it to handle whitespace.
>
> The 'r' in fopen simply means that you want to open that file for reading. If it was a 'w' it would open it for writing. Again, there is a list of these arguments in the documentation.
>
> Regarding your additional question about how to handle multiple files you can do it like this:
>
> a{1}=textscan(fid,...)
> a{2}=textscan(fid2,...)
> etc.
>
> If you have many files you many wish to put this into a loop e.g.
>
> for i=1:number_of_files
> fid=fopen('file.txt','r');
> a{i}=textscan(fid,...);
> fclose(fid);
> end

ok so I was able to figure it out and convert the multiple text files into the matrices. However I was just asked to change it so that everytime matlab sees the character "_" it will move the text after that in that same line into the next column. All other lines after that will stay in the first column unless it also has a "_". How would I go about doing this so that the matrix will also format properly for text2, text3, etc? Text2 should start in the column that does not have any data in it, which should be the number of columns +1 of the last line with the most _ in it. So given that one line has 8 "_" in text1, text2 should start in column 9. I hope this is not too confusing..Thanks all!

Subject: Importing Text Data To Matrix

From: Dave Brackett

Date: 4 Aug, 2009 17:34:02

Message: 7 of 12

"D Nguyen" <dnguyen1022@gmail.com> wrote in message <h59nb1$mpb$1@fred.mathworks.com>...
> "Dave Brackett" <davebrackett@hotmail.com> wrote in message <h4rtp1$f37$1@fred.mathworks.com>...
> > "Matt Fig" <spamanon@yahoo.com> wrote in message <h4r8th$pss$1@fred.mathworks.com>...
> > > "D Nguyen" <dnguyen1022@gmail.com> wrote in message
> > > > Thank you for your reply, however I don't simply want to copy and paste. I wish to understand this function a bit better.
> > > > What does the 'r' do in the fopen function?
> > > > What about the (..,'delimiter', '', 'whitespace', '') for the textscan function?
> > > > Also if I wanted to import mutiple text file of the same format, except have file 1 in column 1, file 2 in column 2, how would I go about doing that? Thanks again!
> > >
> > > If you wish to understand what those functions do, and the arguments they take, look to the help and documentation.
> > >
> > > >>help textscan
> > > >>doc textscan
> > > >>help fopen
> > > >>doc fopen
> >
> >
> > While the textscan documentation can be daunting as it is a very flexible and powerful function don't be put off. There are tables on that page with different parameters that can be set to achieve what you want, e.g. which delimiter you want, and how you want it to handle whitespace.
> >
> > The 'r' in fopen simply means that you want to open that file for reading. If it was a 'w' it would open it for writing. Again, there is a list of these arguments in the documentation.
> >
> > Regarding your additional question about how to handle multiple files you can do it like this:
> >
> > a{1}=textscan(fid,...)
> > a{2}=textscan(fid2,...)
> > etc.
> >
> > If you have many files you many wish to put this into a loop e.g.
> >
> > for i=1:number_of_files
> > fid=fopen('file.txt','r');
> > a{i}=textscan(fid,...);
> > fclose(fid);
> > end
>
> ok so I was able to figure it out and convert the multiple text files into the matrices. However I was just asked to change it so that everytime matlab sees the character "_" it will move the text after that in that same line into the next column. All other lines after that will stay in the first column unless it also has a "_". How would I go about doing this so that the matrix will also format properly for text2, text3, etc? Text2 should start in the column that does not have any data in it, which should be the number of columns +1 of the last line with the most _ in it. So given that one line has 8 "_" in text1, text2 should start in column 9. I hope this is not too confusing..Thanks all!


To me this seems to be a strange thing to want to do. What does your data look like? Anyway, you could achieve something like I think you want like this:

example_data={{'bob hello_283994 yeh_ 2 3';'onetwo';'this is _ sample text _ _ _'}} % as it would be read in using textscan

% for each line:
for i=1:size(example_data{1},1)
    S=strfind(example_data{1}{i},'_');
    if ~isempty(S)
        no_chars=length(example_data{1}{i});
        no_underscores=length(S);
        for j=1:no_underscores
            if j==1
                start_pos=1;
            else
                start_pos=end_pos+1;
            end
            end_pos=S(j);
            new_line{i,j}=example_data{1}{i}(start_pos:end_pos);
        end
        if start_pos~=no_chars
            start_pos=end_pos+1;
            new_line{i,j+1}=example_data{1}{i}(start_pos:no_chars);
        end
    else
        new_line(i,1)=example_data{1}(i);
    end
end
new_line

It is basically just searching for underscores and then splitting up the line at the positions of these underscores. It is no doubt not the best way to do it but it works. There is probably a much neater way using delimiters or string tokens. Hope that helps.

Subject: Importing Text Data To Matrix

From: D Nguyen

Date: 5 Aug, 2009 16:35:19

Message: 8 of 12

"Dave Brackett" <davebrackett@hotmail.com> wrote in message <h59rea$ql1$1@fred.mathworks.com>...
> "D Nguyen" <dnguyen1022@gmail.com> wrote in message <h59nb1$mpb$1@fred.mathworks.com>...
> > "Dave Brackett" <davebrackett@hotmail.com> wrote in message <h4rtp1$f37$1@fred.mathworks.com>...
> > > "Matt Fig" <spamanon@yahoo.com> wrote in message <h4r8th$pss$1@fred.mathworks.com>...
> > > > "D Nguyen" <dnguyen1022@gmail.com> wrote in message
> > > > > Thank you for your reply, however I don't simply want to copy and paste. I wish to understand this function a bit better.
> > > > > What does the 'r' do in the fopen function?
> > > > > What about the (..,'delimiter', '', 'whitespace', '') for the textscan function?
> > > > > Also if I wanted to import mutiple text file of the same format, except have file 1 in column 1, file 2 in column 2, how would I go about doing that? Thanks again!
> > > >
> > > > If you wish to understand what those functions do, and the arguments they take, look to the help and documentation.
> > > >
> > > > >>help textscan
> > > > >>doc textscan
> > > > >>help fopen
> > > > >>doc fopen
> > >
> > >
> > > While the textscan documentation can be daunting as it is a very flexible and powerful function don't be put off. There are tables on that page with different parameters that can be set to achieve what you want, e.g. which delimiter you want, and how you want it to handle whitespace.
> > >
> > > The 'r' in fopen simply means that you want to open that file for reading. If it was a 'w' it would open it for writing. Again, there is a list of these arguments in the documentation.
> > >
> > > Regarding your additional question about how to handle multiple files you can do it like this:
> > >
> > > a{1}=textscan(fid,...)
> > > a{2}=textscan(fid2,...)
> > > etc.
> > >
> > > If you have many files you many wish to put this into a loop e.g.
> > >
> > > for i=1:number_of_files
> > > fid=fopen('file.txt','r');
> > > a{i}=textscan(fid,...);
> > > fclose(fid);
> > > end
> >
> > ok so I was able to figure it out and convert the multiple text files into the matrices. However I was just asked to change it so that everytime matlab sees the character "_" it will move the text after that in that same line into the next column. All other lines after that will stay in the first column unless it also has a "_". How would I go about doing this so that the matrix will also format properly for text2, text3, etc? Text2 should start in the column that does not have any data in it, which should be the number of columns +1 of the last line with the most _ in it. So given that one line has 8 "_" in text1, text2 should start in column 9. I hope this is not too confusing..Thanks all!
>
>
> To me this seems to be a strange thing to want to do. What does your data look like? Anyway, you could achieve something like I think you want like this:
>
> example_data={{'bob hello_283994 yeh_ 2 3';'onetwo';'this is _ sample text _ _ _'}} % as it would be read in using textscan
>
> % for each line:
> for i=1:size(example_data{1},1)
> S=strfind(example_data{1}{i},'_');
> if ~isempty(S)
> no_chars=length(example_data{1}{i});
> no_underscores=length(S);
> for j=1:no_underscores
> if j==1
> start_pos=1;
> else
> start_pos=end_pos+1;
> end
> end_pos=S(j);
> new_line{i,j}=example_data{1}{i}(start_pos:end_pos);
> end
> if start_pos~=no_chars
> start_pos=end_pos+1;
> new_line{i,j+1}=example_data{1}{i}(start_pos:no_chars);
> end
> else
> new_line(i,1)=example_data{1}(i);
> end
> end
> new_line
>
> It is basically just searching for underscores and then splitting up the line at the positions of these underscores. It is no doubt not the best way to do it but it works. There is probably a much neater way using delimiters or string tokens. Hope that helps.

Hi, thank you for your help.
There are a few things though that I am unsure of, especially with the end_pos. I commented the program with stuff that I think I know and stuff I am unsure of. Please check over it.

%sets the matrix with the text data
a = textscan(fid,'%s','delimiter', '','whitespace','');

%for loop starting from 1 to...??
for i=1:size(a{1},1)
    %finds all occurrences of _ and stores each index
    S=strfind(a{1}{i},'_');
    
    %if empty of S
    if ~isempty(S)
        
        %set no_chars to equal length of the data
        no_chars=length(a{1}{i});
       
        %set no_underscores to length of S
        no_underscores=length(S);
        
        %for 1 up to no_underscores
        for j=1:no_underscores
            
            if j==1
                %starting position at 1
                start_pos=1;
                
            %Where does end_pos come from? What is it its purpose?
            else
                start_pos=end_pos+1;
            end
            end_pos=S(j);
            %what is new_line for?
            new_line{i,j}=a{1}{i}(start_pos:end_pos);
        end
        if start_pos~=no_chars
            start_pos=end_pos+1;
            new_line{i,j+1}=a{1}{i}(start_pos:no_chars);
        end
    else
        new_line(i,1)=a{1}(i);
    end
end
%why is there only new_line at the end? What does that do?
new_line

Thank you!

Subject: Importing Text Data To Matrix

From: D Nguyen

Date: 5 Aug, 2009 16:52:02

Message: 9 of 12


> Hi, thank you for your help.
> There are a few things though that I am unsure of, especially with the end_pos. I commented the program with stuff that I think I know and stuff I am unsure of. Please check over it.
>
> %sets the matrix with the text data
> a = textscan(fid,'%s','delimiter', '','whitespace','');
>
> %for loop starting from 1 to...??
> for i=1:size(a{1},1)
> %finds all occurrences of _ and stores each index
> S=strfind(a{1}{i},'_');
>
> %if empty of S
> if ~isempty(S)
>
> %set no_chars to equal length of the data
> no_chars=length(a{1}{i});
>
> %set no_underscores to length of S
> no_underscores=length(S);
>
> %for 1 up to no_underscores
> for j=1:no_underscores
>
> if j==1
> %starting position at 1
> start_pos=1;
>
> %Where does end_pos come from? What is it its purpose?
> else
> start_pos=end_pos+1;
> end
> end_pos=S(j);
> %what is new_line for?
> new_line{i,j}=a{1}{i}(start_pos:end_pos);
> end
> if start_pos~=no_chars
> start_pos=end_pos+1;
> new_line{i,j+1}=a{1}{i}(start_pos:no_chars);
> end
> else
> new_line(i,1)=a{1}(i);
> end
> end
> %why is there only new_line at the end? What does that do?
> new_line
>
> Thank you!
Also I forgot to add..(I didn't see an edit button)
I was also wondering if its possible to modify this so it can remove the underscores when writing to a matrix?

Subject: Importing Text Data To Matrix

From: Dave Brackett

Date: 5 Aug, 2009 19:16:02

Message: 10 of 12

"D Nguyen" <dnguyen1022@gmail.com> wrote in message <h5cdbi$7vm$1@fred.mathworks.com>...
>
> > Hi, thank you for your help.
> > There are a few things though that I am unsure of, especially with the end_pos. I commented the program with stuff that I think I know and stuff I am unsure of. Please check over it.
> >
> > %sets the matrix with the text data
> > a = textscan(fid,'%s','delimiter', '','whitespace','');
> >
> > %for loop starting from 1 to...??
> > for i=1:size(a{1},1)
> > %finds all occurrences of _ and stores each index
> > S=strfind(a{1}{i},'_');
> >
> > %if empty of S
> > if ~isempty(S)
> >
> > %set no_chars to equal length of the data
> > no_chars=length(a{1}{i});
> >
> > %set no_underscores to length of S
> > no_underscores=length(S);
> >
> > %for 1 up to no_underscores
> > for j=1:no_underscores
> >
> > if j==1
> > %starting position at 1
> > start_pos=1;
> >
> > %Where does end_pos come from? What is it its purpose?
> > else
> > start_pos=end_pos+1;
> > end
> > end_pos=S(j);
> > %what is new_line for?
> > new_line{i,j}=a{1}{i}(start_pos:end_pos);
> > end
> > if start_pos~=no_chars
> > start_pos=end_pos+1;
> > new_line{i,j+1}=a{1}{i}(start_pos:no_chars);
> > end
> > else
> > new_line(i,1)=a{1}(i);
> > end
> > end
> > %why is there only new_line at the end? What does that do?
> > new_line
> >
> > Thank you!
> Also I forgot to add..(I didn't see an edit button)
> I was also wondering if its possible to modify this so it can remove the underscores when writing to a matrix?



'end_pos' stands for end position and is just the character index in the string that is directly before the next underscore (i.e. the end of that portion of the string). It is defined after the second if loop.

'new_line' is a cell array which stores the unaltered lines with no underscores and the lines with underscores that were split up. 'new_line' is written at the end of the code simply to display the new_line cell array to the screen. It doesn't actually change anything.

The underscores could be removed from the split portions by setting 'end_pos' to be 1 character shorter than it is currently. The start_pos would then need to be changed to 1 character further along than it is currently (i.e. end_pos+2). Lines to changes are:

start_pos=end_pos+1; to start_pos=end_pos+2;
end_pos=S(j); to end_pos=S(j)-1
start_pos=end_pos+1; to start_pos=end_pos+2; % don't forget to change this one too!

Finally, the first comment below indicates you are unsure what size(a{1},1) is. This is simply the number of lines in a. So it is iterating through each line in a and splitting up each line with underscores. Hope that makes things clearer. I should have commented it originally, but I get lazy with them!

Subject: Importing Text Data To Matrix

From: D Nguyen

Date: 6 Aug, 2009 16:50:19

Message: 11 of 12

"Dave Brackett" <davebrackett@hotmail.com> wrote in message <h5clpi$2kr$1@fred.mathworks.com>...
> "D Nguyen" <dnguyen1022@gmail.com> wrote in message <h5cdbi$7vm$1@fred.mathworks.com>...
> >
> > > Hi, thank you for your help.
> > > There are a few things though that I am unsure of, especially with the end_pos. I commented the program with stuff that I think I know and stuff I am unsure of. Please check over it.
> > >
> > > %sets the matrix with the text data
> > > a = textscan(fid,'%s','delimiter', '','whitespace','');
> > >
> > > %for loop starting from 1 to...??
> > > for i=1:size(a{1},1)
> > > %finds all occurrences of _ and stores each index
> > > S=strfind(a{1}{i},'_');
> > >
> > > %if empty of S
> > > if ~isempty(S)
> > >
> > > %set no_chars to equal length of the data
> > > no_chars=length(a{1}{i});
> > >
> > > %set no_underscores to length of S
> > > no_underscores=length(S);
> > >
> > > %for 1 up to no_underscores
> > > for j=1:no_underscores
> > >
> > > if j==1
> > > %starting position at 1
> > > start_pos=1;
> > >
> > > %Where does end_pos come from? What is it its purpose?
> > > else
> > > start_pos=end_pos+1;
> > > end
> > > end_pos=S(j);
> > > %what is new_line for?
> > > new_line{i,j}=a{1}{i}(start_pos:end_pos);
> > > end
> > > if start_pos~=no_chars
> > > start_pos=end_pos+1;
> > > new_line{i,j+1}=a{1}{i}(start_pos:no_chars);
> > > end
> > > else
> > > new_line(i,1)=a{1}(i);
> > > end
> > > end
> > > %why is there only new_line at the end? What does that do?
> > > new_line
> > >
> > > Thank you!
> > Also I forgot to add..(I didn't see an edit button)
> > I was also wondering if its possible to modify this so it can remove the underscores when writing to a matrix?
>
>
>
> 'end_pos' stands for end position and is just the character index in the string that is directly before the next underscore (i.e. the end of that portion of the string). It is defined after the second if loop.
>
> 'new_line' is a cell array which stores the unaltered lines with no underscores and the lines with underscores that were split up. 'new_line' is written at the end of the code simply to display the new_line cell array to the screen. It doesn't actually change anything.
>
> The underscores could be removed from the split portions by setting 'end_pos' to be 1 character shorter than it is currently. The start_pos would then need to be changed to 1 character further along than it is currently (i.e. end_pos+2). Lines to changes are:
>
> start_pos=end_pos+1; to start_pos=end_pos+2;
> end_pos=S(j); to end_pos=S(j)-1
> start_pos=end_pos+1; to start_pos=end_pos+2; % don't forget to change this one too!
>
> Finally, the first comment below indicates you are unsure what size(a{1},1) is. This is simply the number of lines in a. So it is iterating through each line in a and splitting up each line with underscores. Hope that makes things clearer. I should have commented it originally, but I get lazy with them!

Thank you, I understand the code a bit better now. Now I have to manipulate it so it handles multiple files

%loops through every file with txt extension in directory
for k = 1:length(txtFiles)
    
    %the current file name
    filename = txtFiles(k).name;
    
    %opens files and reads
    fid = fopen(filename,'r');
  
    a = textscan(fid,'%s','delimiter', '','whitespace','');
    
    %sets the column at which the new data should be stored at
    if k==1
        col = 1;
    else col = k * 6;
    end
 
INSERT CODE ABOVE HERE

    %closes the file opened
    fclose(fid);
end

How would I go about manipulating the code that you helped me with, so that it inserts the text data at column col? Every text file has a maxiumum width of 6 columns. I wish for it to store a new text data every 6 columns, except for the first text data. Thanks for your help!

Subject: Importing Text Data To Matrix

From: D Nguyen

Date: 10 Aug, 2009 22:31:03

Message: 12 of 12

I am sorry if I am breaking the rules, but I still need help with this problem, and the thread has gone farr from the first page, so heres a bumpp. Thank you!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us