Writing a text file with information from a data file

1 回表示 (過去 30 日間)
Jonathan
Jonathan 2012 年 6 月 25 日
Hi, I need to be able to take a .dat file that contains 5 strings of the filenames of .jpg images, and out put a .txt file that contains the name of each string and other data for EACH string (i.e. each .jpg image). I figured out how to generate this for one of the strings, but I don't know how to do it for all of the strings. Maybe use some sort of loop?
fileID = fopen('nameOfFile.dat');
filename = fgets(fileID)
fileID2 = fopen('nameOfFile.txt','wt');
info = 'blah blah';
num = 5;
fprintf(fileID2,'Filename: %s\n Info: %s\n Number: %g\n',filename,info,num);
fclose(fileID);
fclose(fid);
--------------------------------------------------------------------------------
UPDATE: I have figured out how to generate this for all of the strings, but I can only do it if I know how many strings there are. I need to be able to do it with any input data file (not just one with 5 strings). Also, I need the info and num to be able to be different for each filename.
fileID = fopen('nameOfFile.txt','wt');
filename = textread('nameOfFile.dat','%s\n')
info = 'blah blah blah';
num = 5;
fprintf(fileID,'Filename: %s\nInfo: %s\nNumber: %g\n',filename{1},info,num,filename{2},info,num,filename{3},info,num,filename{4},info,num,filename{5},info,num);
fclose(fileID);
  1 件のコメント
Jonathan
Jonathan 2012 年 6 月 25 日
I figured it out! It was much simpler than I thought.
fileID = fopen('nameOfFile.txt','wt');
filename = textread('nameOfFile.dat','%s\n');
for i = 1:length(filename)
info = 'blah blah blah';
num(i) = 5;
fprintf(fileID,'Filename: %s\nInfo: %s\nNumber: %g\n\n',filename{i},info,num(i));
end
fclose(fileID);

サインインしてコメントする。

採用された回答

per isakson
per isakson 2012 年 6 月 25 日
This is a hint!
% or a more specific wildcard pattern
sad = dir( fullfile( folder_spec, '*.dat' ) );
file_name_list = permute( { sad.name }, [2,1] ); % row vector
fileID = fopen('nameOfFile.txt','wt');
for file_name = file_name_list % loop over all files
image_file_name = textread( fullfile(folder_spec,file_name{:}, ... );
info = something
num = something else
fprintf( fileID, 'Filename: %s\nInfo: %s\nNumber: %g\n' ...
, image_file_name, info, num )
end
fclose( fileID );
  2 件のコメント
Jonathan
Jonathan 2012 年 6 月 25 日
Sorry, but this doesn't help me very much. I'm still confused.
per isakson
per isakson 2012 年 6 月 25 日
Cannot help without better description of the problem

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by