Save output sequence of files

Hi! I have a question about how can I save a loop outputs. I want to write the output values of a large series of files on a separate line on text.txt. The problem is it only works with the first one, but it doesn't work with the rest. Is there something wrong?
myPath = 'C:\EX\';
a= dir (fullfile(myPath,'*.DIM'));
fileNames = { a.name };
% Rename files
for k = 1:length(fileNames)
newFileName = [fileNames{k}(1:2) fileNames{k}(4:6) '.txt'];
movefile([myPath fileNames{k}], [myPath newFileName]);
end
filePattern=fullfile( myPath,'*.txt');
txtFiles= dir(filePattern);
for k = 1:length(txtFiles)
baseFileName=txtFiles(k).name;
fullFileName= fullfile(myPath,baseFileName);
fid=fopen(fullFileName, 'r');
for i = 1:18
m{i} = fgetl(fid);
end
result2 = m{18};
result2b= result2([12:19]);
fid=fopen(fullFileName, 'r');
for i = 1:30
m{i} = fgetl(fid);
end
result3 = m{30};
result3b= result3([12:19]);
fid=fopen(fullFileName, 'r');
for i = 1:31
m{i} = fgetl(fid);
end
result4 = m{31};
result4b= result4([12:20]);
fid=fopen(fullFileName, 'r');
for i = 1:19
m{i} = fgetl(fid);
end
result5 = m{19};
result5b= result5([12:20]);
text= {baseFileName, result2b, result3b, result4b, result5b};
final= [Fields'; text];
end
I've read about fprint command and I've tried but it doesn't work at all. Really thanks in advance!
EDIT:
I added a screenshoot about my variables. Is it possible because of the txtFiles is wrong?

 Accepted Answer

filePattern = fullfile( myPath,'*.txt');
txtFiles = dir(filePattern);
output = cell(length(txtFiles), 5);
for k = 1:length(txtFiles)
baseFileName = txtFiles(k).name
fullFileName = fullfile(myPath, baseFileName );
fileData = fileread(fullFileName);
fileCell = regexp(fileData, '\n', 'split');
result2 = m{18};
result2b= result2([12:19]);
result3 = m{30};
result3b= result3([12:19]);
result4 = m{31};
result4b= result4([12:20]);
result5 = m{19};
result5b= result5([12:20]);
output(k, :) = {baseFileName, result2b, result3b, result4b, result5b};
end

More Answers (1)

Walter Roberson
Walter Roberson on 6 Nov 2012
You are not closing the files you open.

5 Comments

Thanks Walter, I've closed with fopen command but it only works with the first file, not with the others. It can be a mistake in that part?
text= {baseFileName, result2b, result3b, result4b, result5b};
Matriz_final= [Fields; text];
"it only works for the first file" is not specific enough. Is an error generated?
You do not show any initialization for Fields.
You append text to Fields producing Matrix_final, but that work is going to be overwritten the next iteration of the loop, giving a result which is only for the final file.
Why are you opening and closing the file several times? Why not open it once, read and store up to line 31, close it, and then extract the various m{18}, m{19}, m{31} and so on? It is the same file for all four extracted lines.
Thanks Walter. I mean that I have 3 files in the same folder and I want to do the same process for all of them (extract some information, and then write in a line the 'output' in Matrix_final). But yes, it will be overwritten all the time.
I know what you mean. I've changed the code and now it only open once but the loop doesn't work at all.
Now after your changes we do neither know your program, nor your expectations, nor the occurring error message or whatever "doesn't work" exactly mean. How could we guess an improvement?
Solved with the following post code! ;)

Sign in to comment.

Categories

Asked:

on 31 Oct 2012

Community Treasure Hunt

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

Start Hunting!