Extracting variables from multiple *.txt files and having the variable name dependent on the name of

Discussion in 'MATLAB' started by Alana Smentek, Apr 15, 2010.

  1. I have about 30 *.txt files that have contain data in the same format. I'd like to extract a certain set of data from each of these files, and store them as their own variables, not in a matrix.

    Each file name is structured in the following way:
    name_20xx_yy.txt
    where xx reflects the 2-digit year and yy reflects the 2-digit month of each data set.

    I'd like to be able to create column vectors from the data set that reflect the different data sets that they come from, i.e.

    var_20xx_yy= name_20xx_yy:),3)

    This way I will have 30 different var_xx_yy variables each reflecting the month and year that they came from, i.e. data from February 2009 will be var_2009_02.

    I feel like there should be a way to loop this command so I don't have to create each variable set by using the same line 30 times.

    Does anyone have any suggestions? Thanks!
     
    Alana Smentek, Apr 15, 2010
    #1
    1. Advertisements

  2. Alana Smentek

    Matt J Guest

    ===============

    I don't know why you want to do this, but I'll just point out that it's commonly and emphatically discouraged:

    http://matlabwiki.mathworks.com/MAT...e_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
     
    Matt J, Apr 15, 2010
    #2
    1. Advertisements

  3. Alana Smentek

    TideMan Guest

    Yes, I have a suggestion: don't do this silly thing.

    There are many other better ways to do it, such as cell arrays,
    structures, multi-dimensional arrays, etc.

    If you give us a bit more detail, maybe we can make some suggestions.
    For example, will there be the same number of data from each file?
    How many variables from each file?
    etc
     
    TideMan, Apr 15, 2010
    #3
  4. Alana Smentek

    Alana Guest

    I am trying to do this so that I can pull a vector from each *.txt file, and then manipulate that vector (3690 x 1) into a matrix (45 X 82). Maybe there's a way to pull all of the specific vectors from the *.txt files and put them into one matrix, and then manipulate each column of the matrix into its own unique matrix (30 different matrices from 30 different files).

    I'd like to have each of these 30 matrices have a name that reflects the *.txt file it came from, and while I don't understand why that is highly discouraged, I suppose it really doesn't matter what the name of each matrix is.
     
    Alana, Apr 15, 2010
    #4
  5. Alana Smentek

    TideMan Guest

    But WHY do you need the name to reflect the .txt it came from?
    Because, by doing this, you forever consign yourself to manually
    manipulating the matrices.
    Whereas, if they are referred to by an index (say, the chronological
    order), then you will be able to process them in a loop for example.
    When you're dealing with one or two matrices, manual manipulation is
    not a problem, but you're talking about 30 of them. You need to think
    of how you are going to deal with all these matrices later.
     
    TideMan, Apr 15, 2010
    #5
  6. Alana Smentek

    Alana Guest

    If you can't tell, I'm slightly new with Matlab...so try and bear with me.

    It doesn't matter if each matrix has a name that reflects the *.txt name. I'll just have to manually label each plot so that it reflects the file name that it came from because at some point I'm going to have to show where each data set came from.

    To get really specific, each *.txt file has 7 columns, and 3690 rows. Say I want to create a matrix that has column 5 from each *txt file populating the matrix, so it would be a 30 column 3690 matrix. How would I do that?

    From there, I need to manipulate each column into a matrix (45 x 82). Can I store each of these matrices into another matrix? (Is this what you mean by multiple-dimension matrices?)
     
    Alana, Apr 15, 2010
    #6
  7. Alana Smentek

    Matt J Guest

    ===================

    I would recommend something like the following, which assumes that your .txt files are delimited by spaces and that your filenames are stored in cell array of strings called FileNames. e.g.

    FileNames = {'myFile1','myFile2',....'myFile30'};

    for ii=1:30

    data=dlmread(FileNames{ii},' ','E1..E3690'); %read 5th column of file

    S(ii).filename=FileNames{ii};
    S(ii).matrix=reshape(data,[45,82]);

    end

    Now, as TideMan was telling you, you can plot each data set programmatically rather than manually, by looping through the struct array S

    for ii=1:30

    plot(...); %Use S(ii).matrix for the plot

    title(['This data was from the file called ' S(ii)])

    end
     
    Matt J, Apr 15, 2010
    #7
  8. Alana Smentek

    Matt J Guest

    ======

    That should have read

    title(['This data was from the file called ' S(ii).filename])
     
    Matt J, Apr 15, 2010
    #8
  9. Alana Smentek

    TideMan Guest

    Because the matrix from each file is the same size, you can store them
    in a large array with dimensions 3690x7x30.
    Now, if you want data from,say, the 21st file, you just address it
    like this:
    y=data:),:,21);
    Then you can reshape it like this:
    y=reshape(y,45,82,7);
    or you could reshape the big array:
    data=reshape(data,45,82,7,30);
    and access the data from 21st file like this
    y=data:),:,:,21);

    If you have used dir to get the filenames like this:
    d=dir('pth_to_files\*.txt');
    then the name of the 21st file will be in the structure array:
    d(21).name

    So you see you don't need to use the file name as the variable name
    because it will always be available to you.
     
    TideMan, Apr 15, 2010
    #9
  10. Alana Smentek

    Alana Guest

    Thanks for the help!
     
    Alana, Apr 16, 2010
    #10
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.