Pergunta

I have > 10 text files that I've read into a variable "files" using dir(*.txt). The files consist of matrices of varying dimensions on the order of 100x1000. The filenames for each are of the same format (L\d{1,2}N_20\d{2}.txt), so, with the brilliant help of an ephemeral user, I was able to implement quite a nice routine to dynamically generate fieldnames within a nested structure from parts of the filenames. This has gone a million miles in the right direction for helping me towards my final goal, but one mystery remains: in my hero's last block of code, he suggested I write:

for ix_id = 1:length(ids)
  for ix_year=1:length(ids_years{ix_id})
    data.(ids{ix_id}).(['Y' ids_years{ix_id}{ix_year}]) = ...
                         'read in my data here for each one';
  end
end

Unfortunately, I literally tried to use regexp and structures for the first time yesterday. That code creates a number of structure fields equal to my number of files that I'd like to load, and investigation reveals that

names=fieldnames(data)

gives

names =
    {
       [1,1] = L14N
       [2,1] = L6N
    }

and

names2=fieldnames(data.L14N)

gives

names2 =
{
   [1,1] = Y2009103
   [2,1] = Y2010087
}

which is exactly I wanted, but I just don't understand how to 'read in my data here...'. I'd like to put the file that has L14N and 2009103 in its name in the portion of the structure that's identified as data.L14N.Y2009103 and so on. Since it appears that my data read in in the correct order, I tried adding an outer loop:

for jx = 1:numel(files)
  for ix_id=1:length(ids)
    for ix_year=1:length(ids_years{ix_id})
      data.(ids{ix_id}).(['Y' ids_years{ix_id}{ix_year}]) = dlmread(files{jx})
    end
  end
end

but, of course, that resulted in all of the fields of the structure containing only the matrix found in files{numel(files)} (i.e., the last iteration of my loop).

At the risk of asking a bad question, could someone help me to break down the way in which I should read in the data? This is all an elaborate ploy to avoid eval, but I'm starting to wonder if I'm crazy to try that.

Foi útil?

Solução

You already got the name, but split in several parts.

ids{ix_id} is for example L6N

['Y' ids_years{ix_id}{ix_year}] is for example Y2010087

To get back the file name, put these parts together:

[ids{ix_id} '_' ids_years{ix_id}{ix_year} '_2000MHZ.txt']

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top