How to sort data files without actually writing these files
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
dpb
2022-10-11
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.
采纳的回答
dpb
2022-10-11
Well, maybe I'll take a shot at it and see if get the idea...
First a comment on the existing code -- in
...
l = fgetl(FID)
exist Fid2;
if true(ans)
fclose(Fid2);
...
don't "ever" rely on the default ans variable as the result in a script/function -- it's handy playing at the command line, but very risky practice in code...particularly as above you put the trailing ";" on the code line, there won't have been a return value written for that instruction, but from somewhere/something already in the ans variable.
Write instead
if exist('Fid2')
fclose(Fid2);
...
but testing for open file handles using
if Fid2>0
...
is better than testing for the variable; the variable still exists even if fclose the file handle unless clear the variable as well in second step; closing the file handle does NOT remove (or even change the value of) the file handle variable unless one writes
Fid2=fclose(Fid2);
to update the variable value and that still doesn't remove the variable itself from the workspace.
Anyway, side excursion out of the way, let's see about the original Q?...
file=readlines('abc.txt'); % bring the whole file into memory
file=file(~startsWith(file,'**')); % eliminate the comment lines
writelines(file,'NewOutputFile.txt') % output text file of input w/o comments (*)
Set your desired output file name as wanted, of course, in place of the above string.
It appears the above should be what was asked for from reading the preceding code as a text file mimicking the input file.
(*) writelines is a relatively recent addition -- R2021a I think, maybe. Use writematrix if you're using earlier release
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Large Files and Big Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!