Print Out Txt File Column Data

조회 수: 2 (최근 30일)
Tyann Hardyn
Tyann Hardyn 2021년 6월 24일
편집: Tyann Hardyn 2021년 6월 27일
Hi, i wanna ask...
Im just learn about how to read a txt file and write some column data from it into another txt files. So i have these datas in my original txt file :
2019 05 03 00 00 59 39296.64 154.74 -9052.05 39296.94 0.003938 -0.226401 40326.04
2019 05 03 00 01 59 39296.68 155.35 -9052.87 39296.98 0.003953 -0.226420 40326.26
2019 05 03 00 02 59 39296.95 155.96 -9053.74 39297.26 0.003969 -0.226440 40326.73
2019 05 03 00 03 59 39297.37 155.82 -9054.47 39297.68 0.003965 -0.226455 40327.30
2019 05 03 00 04 59 39297.34 154.31 -9053.50 39297.64 0.003927 -0.226432 40327.04
2019 05 03 00 05 59 39296.21 151.67 -9050.10 39296.50 0.003859 -0.226356 40325.17
2019 05 03 00 06 59 39296.17 150.15 -9049.32 39296.46 0.003821 -0.226338 40324.95
The data above are without header. And, i want to write it again BUT i just need the data in 3rd, 4th, 5th (time data in hour, minute, and second), and 10th (magnetic data in nano tesla) column (from left) and write the data in another txt file.
In Matlab Guide (GUI), i have just write these codes :
formku = guidata(gcbo);
[namafile1,direktori]=uigetfile('*.txt','Load Magnet Data LEMI');
eval(['cd ''' direktori ''';']);
eval(['dataku=load(''' namafile1 ''')']);
[namafile2, direktori] = uiputfile('*.txt', 'save as')
eval(['cd ''' direktori ''';']);
namafile2=[namafile2]
fout=fopen(namafile2,'w');
tahun = dataku(:,1); %: Mendapatkan data tahun pada kolom 1
bulan = dataku(:,2); %: Mendapatkan data bulan pada kolom 2
tanggal = dataku(:,3); %: Mendapatkan data tanggal pada kolom 3
jam = dataku(:,4); %: Mendapatkan data jam pada kolom 4
menit = dataku(:,5); %: Mendapatkan data menit pada kolom 4
detik = dataku(:,6); %: Mendapatkan data detik pada kolom 6
xmagnet = dataku(:,7); %: Mendapatkan data magnet arah sumbu x pada kolom 7
ymagnet = dataku(:,8); %: Mendapatkan data magnet arah sumbu y pada kolom 8
zmagnet = dataku(:,9); %: Mendapatkan data magnet arah sumbu z pada kolom 9
h = dataku(:,10); %: Mendapatkan data medan magnet Komponen H pada kolom 10
w = (3600*jam)+(60*menit)+ detik;
kolom = [jam; menit; detik];
xi =[min(w):1:max(w)];
fprintf(fout,'%8s %8s\n','Waktu','Komponen H Magnet');
for i=1:length(h)
fprintf(fout,'%d %.2f\n ', kolom, h);
end;
fclose(fout);
The above code i write is doesnt work well by using 2 or more column (im sucessful if it just 1 column, ex : h = dataku(:,10) in column 10), because i want to put the 3rd, 4th, and 5th column right before the 10th column so that the 3rd column become 1st column, 4th column become 2nd column, 5th column become 3rd column, and 10th column become 4th column in my new txt files (re-write).
So in this case, i just want to open an original txt file (attached) contained with those datas and then re-write the data inside but only with a specific column i want to choose.... Please help me out.... Thank you very much...
  댓글 수: 6
Tyann Hardyn
Tyann Hardyn 2021년 6월 24일
Thank you very much !
Tyann Hardyn
Tyann Hardyn 2021년 6월 27일
편집: Tyann Hardyn 2021년 6월 27일
Answer was accepted

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by