Help rewritting textfiles in a certain way
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Zaid Khalil
il 18 Giu 2021
Commentato: Zaid Khalil
il 19 Giu 2021
So I currently have text files that look like this for example, but can go on for a lot longer:
14 2.649 5.000 93.5020 -61.9602 2.5000 90.5132 -64.4065 2.5000 88.3552 -67.5184 2.5000 87.8311 -70.6759 2.5000 88.0976 -74.0126 2.5000 90.2142 -75.7097 2.5000 92.9562 -77.3048 2.5000 95.6058 -75.8448 2.5000 98.5412 -74.1425 2.5000 99.7479 -70.8020 2.5000 100.8159 -67.0142 2.5000 100.0439 -63.0305 2.5000 98.6425 -58.8215 2.5000 95.7429 -55.5767 2.5000
#
14 2.040 5.000 -79.9068 55.3230 2.5000 -84.3332 53.5374 2.5000 -88.8590 51.4109 2.5000 -93.3437 49.3544 2.5000 -97.6597 47.7654 2.5000 -101.6949 47.0018 2.5000 -105.5733 47.2205 2.5000 -108.7795 48.8558 2.5000 -111.8250 51.5986 2.5000 -114.0240 55.1797 2.5000 -116.2758 59.4346 2.5000 -118.2519 63.7965 2.5000 -120.4604 68.1404 2.5000 -122.8527 71.9415 2.5000
and the goal is to make it so that the first set of numbers is reordered like this:
14.000000 2.649000 5.000000
93.502000 -61.960200 2.500000
90.513200 -64.406500 2.500000
88.355200 -67.518400 2.500000
87.831100 -70.675900 2.500000
88.097600 -74.012600 2.500000
90.214200 -75.709700 2.500000
92.956200 -77.304800 2.500000
95.605800 -75.844800 2.500000
98.541200 -74.142500 2.500000
99.747900 -70.802000 2.500000
100.815900 -67.014200 2.500000
100.043900 -63.030500 2.500000
98.642500 -58.821500 2.500000
95.742900 -55.576700 2.500000
and placed into a new text file, then have matlab read the '#' and open another new file to do the same thing with the second set of numbers.
Any help is appreciated, especially similar examples.
2 Commenti
Scott MacKenzie
il 19 Giu 2021
It might help if you post one of the text files, or a subset of the file.
Risposta accettata
Scott MacKenzie
il 19 Giu 2021
Modificato: Scott MacKenzie
il 19 Giu 2021
This can probably be streamlined, but I think it achieves what you are looking for:
% read the test data into a 1x1 cell and convert to string array
fid = fopen('testdata.txt'); % data you posted
C = textscan(fid, '%s');
fclose(fid);
s = string(C{:});
% find indices of hash delimiters
idx = [0; find(strcmp(s(:), '#'))];
% pull data between delimiters, reshape, write to output file
k = 1;
for i=2:length(idx)
s1 = s(idx(i-1)+1:idx(i)-1);
s2 = reshape(s1,3,[])';
filename = sprintf('somefile%d.txt', k);
writematrix(s2,filename);
k = k+1;
end
3 Commenti
Scott MacKenzie
il 19 Giu 2021
OK, yes. There was a bug in the reshaping. I just edited the answer with a fix. Try again, please.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Data Import and Export in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!