Using for loop to enter data with similar names

1 visualizzazione (ultimi 30 giorni)
jfrazie9
jfrazie9 il 26 Mar 2018
Risposto: Jan il 28 Mar 2018
I am trying to import data from 250 *.txt files. The file names are as such "C58501_line_ssf_001" to "C58501_line_ssf_250".
numfiles = 250;
filenames = 'C58501_line_ssf_%.txt'
results = cell(numfiles, 1);
for K = 1 : numfiles
modpath = filenames{K};
datastruct = load(modpath);
end
these files have multiple data rows for 55 individual paths. I want to import these *.txt files, extract the data from paths 16-20 and then get the individual values from 3 seperate columns. I am very uncertain on how to import all the data files and eliminate data down to the rows and columns I need.
Thank you in advance!
  7 Commenti
jfrazie9
jfrazie9 il 27 Mar 2018

I began there from other questions asked and got something that created 1 7909x34 double when I need 250 7909x10 doubles using the following code

for k = 1:250

    textFilename = sprintf('C58501_line_ssF_%04d.txt',k);
    M = dlmread(textFilename,' ',1,0);
end

I would like to get these files in as 250 files as 7909x10 doubles and reduce them all to 7909x4 where the data form columns 1,2,3 and 6 are pulled out. After that I need to reduce it further so that all the values in column 1 with values of 16,17,18,19 and 20 are pulled out this should result in approximately a 500x4 double. I know what I am after I just have not been able to get an answer from looking at matlab tutorials and questions asked previously.

Any help is again greatly appreciated.

Bob Thompson
Bob Thompson il 27 Mar 2018
See comments on your other question.
For gathering multiple files you can also look at using the uigetfile() command. It only allows you to select from one directory at a time, but you are able to pick multiple files which it will index in a cell array of strings.
If the files are not able to be moved to a single directory then changing the string using sprintf and a for loop like you have is probably your best bet.

Accedi per commentare.

Risposte (1)

Jan
Jan il 28 Mar 2018
C = cell(1, 250);
for k = 1:250
Filename = sprintf('C58501_line_ssF_%04d.txt',k);
M = dlmread(Filename,' ',1,0);
C{k} = M(ismember(M(:,1), 16:20), [1,2,3,6]);
end
Result = cat(1, C{:});

Categorie

Scopri di più su Workspace Variables and MAT-Files in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by