Reputation: 21
I am not an expert in matlab and use it for simple calculations in physics. I have never tried data mining with it. However, at the moment, I want to split data text file with equal elements in the row into several files. The blocks of the text file that needs to be separated into several files are separated by a blank line. I want to store the information in arrays. The test data set can be found here
https://drive.google.com/file/d/1lRuQIx0QzVhMFtROQOJC3uJcg_Fbvfoq/view?usp=sharing
I am trying to create a small script that writes all blocks in the original file into separate output files that contain only one block. Ideally these files would be named as the name as data1.txt data2.txt and so on where 1 and 2 are the number of blocks. Any help/suggestion is greatly appreciated.
Upvotes: 2
Views: 89
Reputation: 4767
Here's a function that I've created that can be called by:
Here's the function uploaded to GitHub
File_Name = "Tryout.txt";
Subsection_Text(File_Name);
The code below isn't the most concise but it should do the trick. It parses the text file line by line using the function fgetl()
. The blank lines are used as delimiters for starting a new file. The tricky part involved the way MATLAB can evaluate spaces as a character. Using the fact that every line in this data set either has an X, Y or Z
character sets the definition for what constitutes as a line.
clear;
clc;
Mode = 'rt';
File_ID = fopen("Tryout.txt",Mode);
Break_Condition = 0;
Line_Index = 1;
while(Break_Condition < 3)
Text = string(fgetl(File_ID));
Empty = ~contains(Text,["X" "Y" "Z"]);
if(~Empty)
Break_Condition = 0;
Lines(Line_Index,1) = Text;
end
if(Empty)
Break_Condition = Break_Condition + 1;
Lines(Line_Index,1) = "";
end
Line_Index = Line_Index + 1;
end
Lines = Lines(1:length(Lines)-3);
fclose(File_ID);
Break_Condition = 0;
Block_Index = 1;
Save_File_ID = fopen("data" + num2str(Block_Index) +".txt",'w');
for Line_Index = 1: length(Lines)
Print_Text = Lines(Line_Index,1);
if(Print_Text ~= "")
Break_Condition = 0;
fprintf(Save_File_ID,"%s",Lines(Line_Index,1));
fprintf(Save_File_ID,"\n");
end
if(Print_Text == "")
Break_Condition = Break_Condition + 1;
if(Break_Condition == 2)
fprintf("Start new file\n");
Block_Index = Block_Index + 1;
Save_File_ID = fopen("data" + num2str(Block_Index) +".txt",'w');
end
end
1 S X 0.00026468 0.05840333 -0.00000503 0.00000220 -0.00006671
Y 0.01038702 -0.00000313 0.00000194 0.00000252 0.05330246
Z -0.00000018 0.00000340 0.04070305 0.00007263 -0.00000140
2 S X 0.00014171 0.03011383 0.00010166 -0.05798360 -0.00000669
Y 0.04058891 -0.00017498 0.00020915 -0.00000008 0.01402693
Z -0.03503189 0.00020203 0.04033549 0.00007595 0.04355313
3 S X 0.00014261 0.03010885 -0.00010647 0.05798668 -0.00001234
Y 0.04058889 -0.00017504 -0.00020776 0.00000080 0.01402683
Z 0.03503150 -0.00019534 0.04033281 0.00007114 -0.04355608
1 S X 0.04356337 -0.00000001 0.02077084 0.00000015 0.00000034
Y 0.00002152 -0.00000006 0.00000140 0.00000007 0.00877642
Z 0.00000016 0.00000034 0.00000006 -0.01895397 -0.00000002
2 S X -0.03932055 0.00258729 -0.03252282 0.00000100 0.00000117
Y 0.00000868 -0.00000106 0.00000134 -0.03714789 0.00961379
Z 0.00001393 0.00000251 -0.00000096 0.05653194 -0.05987231
3 S X -0.03931981 -0.00258731 -0.03252280 -0.00000137 0.00000116
Y 0.00000868 0.00000102 0.00000088 0.03714794 0.00961382
Z -0.00001360 0.00000259 0.00000015 0.05653178 0.05987235
1 S X 0.00000001 -0.00000000 0.00000000 0.00000003 0.00000000
Y -0.00000002 0.00000000 -0.00000000 -0.00993526 0.00000000
Z -0.03154647 0.00000025 0.02119553 0.00000000 0.00793086
2 S X 0.00000002 0.01626500 -0.00000011 0.00000003 -0.00000006
Y 0.04786716 -0.00000021 -0.02203705 0.01792716 0.02061079
Z 0.00338406 0.00000018 0.03888413 -0.02145517 0.01502515
3 S X -0.00000002 -0.01626500 0.00000011 0.00000003 0.00000006
Y -0.04786706 0.00000021 0.02203705 0.01792717 -0.02061079
Z 0.00338411 0.00000018 0.03888412 0.02145518 0.01502514
1 S X 0.00026468 0.05840333 -0.00000503 0.00000220 -0.00006671
Y 0.01038702 -0.00000313 0.00000194 0.00000252 0.05330246
Z -0.00000018 0.00000340 0.04070305 0.00007263 -0.00000140
2 S X 0.00014171 0.03011383 0.00010166 -0.05798360 -0.00000669
Y 0.04058891 -0.00017498 0.00020915 -0.00000008 0.01402693
Z -0.03503189 0.00020203 0.04033549 0.00007595 0.04355313
3 S X 0.00014261 0.03010885 -0.00010647 0.05798668 -0.00001234
Y 0.04058889 -0.00017504 -0.00020776 0.00000080 0.01402683
Z 0.03503150 -0.00019534 0.04033281 0.00007114 -0.04355608
1 S X 0.04356337 -0.00000001 0.02077084 0.00000015 0.00000034
Y 0.00002152 -0.00000006 0.00000140 0.00000007 0.00877642
Z 0.00000016 0.00000034 0.00000006 -0.01895397 -0.00000002
2 S X -0.03932055 0.00258729 -0.03252282 0.00000100 0.00000117
Y 0.00000868 -0.00000106 0.00000134 -0.03714789 0.00961379
Z 0.00001393 0.00000251 -0.00000096 0.05653194 -0.05987231
3 S X -0.03931981 -0.00258731 -0.03252280 -0.00000137 0.00000116
Y 0.00000868 0.00000102 0.00000088 0.03714794 0.00961382
Z -0.00001360 0.00000259 0.00000015 0.05653178 0.05987235
1 S X 0.00000001 -0.00000000 0.00000000 0.00000003 0.00000000
Y -0.00000002 0.00000000 -0.00000000 -0.00993526 0.00000000
Z -0.03154647 0.00000025 0.02119553 0.00000000 0.00793086
2 S X 0.00000002 0.01626500 -0.00000011 0.00000003 -0.00000006
Y 0.04786716 -0.00000021 -0.02203705 0.01792716 0.02061079
Z 0.00338406 0.00000018 0.03888413 -0.02145517 0.01502515
3 S X -0.00000002 -0.01626500 0.00000011 0.00000003 0.00000006
Y -0.04786706 0.00000021 0.02203705 0.01792717 -0.02061079
Z 0.00338411 0.00000018 0.03888412 0.02145518 0.01502514
Upvotes: 0