How to sort data files without actually writing these files
Show older comments
I wanted to sort a text file based on double Astrisk(**) Mark,
I came up with this code in order to sort the file, But by using this code, it writes separate .txt files which creates many number of files. So what change should be made in the Code so that it wont write separate files and Directly present the data in the workbench or .xlsx File.
FID=fopen('abc.txt','rt');
%FID=fopen(data);
numberoflines = 44;
for i=1:numberoflines
l = fgetl(FID)
if strcmp(l(1:2),'**')
target = l(3:end);
l = fgetl(FID)
exist Fid2;
if true(ans)
fclose(Fid2);
end
Fid2 = fopen(target,'w');
end
fprintf(Fid2,[l '\n'])
end
close all
% Reading data into workbench
fid = fopen('DATA1', 'rt');
data1 = textscan(fid, '%s %s %s %s', 'CollectOutput', 1);
fclose(fid);
data1 = data1{1};
fid = fopen('COMMENTS', 'rt');
Comments = textscan(fid, '%s %s %s %s', 'CollectOutput', 1);
fclose(fid);
Comments = Comments{1};
fid = fopen('CONSTANTS', 'rt');
Constant = textscan(fid, '%s %s %s %s', 'CollectOutput', 1);
fclose(fid);
Constant = Constant{1};
fid = fopen('DATA_LIST', 'rt');
data_list = textscan(fid, '%s %s %s %s %s %s %s', 'CollectOutput', 1);
fclose(fid);
data_list = data_list{1};
fid = fopen('DATA2', 'rt');
data2 = textscan(fid, '%s %s %s %s %s %s %s', 'CollectOutput', 1);
fclose(fid);
data2 = data2{1}
1 Comment
dpb
on 11 Oct 2022
Not easy to decipher what you're really trying to do here; there's no sorting in the code at all to match the description although there is a splitting of the input file into multiple output files.
But, if the point is only one file, then why do that and what was wrong with the first file as it was?
Attach the input file and then illustrate with it what you're really expecting/wanting for a result.
Accepted Answer
More Answers (0)
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!