Re-writing and Plotting TXT File By Using Push Button GUI in Matlab

1 view (last 30 days)
Hello, everyone.... I need your help to fix my GUI script to re-write and plotting a TXT File by using pushbutton. So i have a matlab script which were run correctly to import a TXT File (attached File) and re-write it in the other column format and also plot its component (DATE + TIME) versus ABKZ. Here is my Correct Matlab Script you can run it over your matlab (by pressing F5) :
close all
clear clc
clc
filename = "abk198911dmin.txt"
opts = detectImportOptions(filename,"NumHeaderLines",12);
opts.SelectedVariableNames = ["DATE","TIME","ABKZ"];
T = readtable(filename,opts);
DATE = T{:,1};
TIME = T{:,2};
TIME.Format = 'hh:mm:ss';
DOY = T{:,3};
d = T.DATE + T.TIME;
p = plot(d, DOY);
[namafile2, direktori] = uiputfile('*.txt', 'SAVE AS')
eval(['cd ''' direktori ''';']);
fout=fopen(namafile2,'w');
for i=1:length(DOY)
fprintf(fout,'%s %.2f\n', char(d(i)), DOY(i));
end
And the ouput should be 2 kind : 1) Plot Figure and 2) the re-writted TXT file from the original file (abk198911dmin.txt) contained with the new formatted data from the above script :
29-Nov-1989 23:31:00 51018.00
29-Nov-1989 23:32:00 50983.00
29-Nov-1989 23:33:00 50987.00
29-Nov-1989 23:34:00 50988.00
29-Nov-1989 23:35:00 50988.00
29-Nov-1989 23:36:00 50992.00
29-Nov-1989 23:37:00 50981.00
29-Nov-1989 23:38:00 51001.00
29-Nov-1989 23:39:00 51037.00
29-Nov-1989 23:40:00 51056.00
29-Nov-1989 23:41:00 51099.00
29-Nov-1989 23:42:00 51137.00
29-Nov-1989 23:43:00 51151.00
29-Nov-1989 23:44:00 51160.00
29-Nov-1989 23:45:00 51166.00
29-Nov-1989 23:46:00 51172.00
29-Nov-1989 23:47:00 51182.00.................................
However, i always fail to load the TXT file data by using pushbutton on GUI. The pushbutton process i want is simple as :
1) Load the TXT File to Matlab
2) Re-write the Data in another TXT File (manual name) by that format i want : "DATE","TIME","ABKZ"
The above process should be done in one click.
This is my Fail Script :
function pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
formku = guidata(gcbo);
[namafile,direktori]=uigetfile('*.txt','Load Data Magnet LEMI Format IAGA','Multiselect','on');
eval(['cd ''' direktori ''';']);
eval(['dataku=load(''' namafile ''')']);
opts = detectImportOptions(dataku,"NumHeaderLines",12);
opts.SelectedVariableNames = ["DATE","TIME","ABKZ"];
T = readtable(dataku,opts);
DATE = T{:,1};
TIME = T{:,2};
TIME.Format = 'hh:mm:ss';
DOY = T{:,3};
d = T.DATE + T.TIME;
w = hours(T.TIME(1:5));
h = hours(T.TIME(1:end));
l = plot(d, DOY);
xlabel('Deret Waktu (Time Series)','fontweight','bold','fontsize',10);
ylabel('Komponen H Magnet Data Lemi Non IAGA Format (nT)','fontweight','bold','fontsize',10);
legend('off');
set(formku.figure1,'CurrentAxes',formku.satu);
set(l,'LineWidth',1);
set(formku.satu,'Color',[1 0.96 0.9],...
'XGrid','on',...
'YGrid','on',...
'NextPlot','add');
set(formku.figure1,'Userdata',dataku);
And for the other pushbutton (lets say pushbutton2), the pushbutton process should be :
1) Load the TXT File to Matlab
2) Plot in a figure (just like the output file of my correct matlab script above)
in a single click...
This is my Fail Script :
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
formku = guidata(gcbo);
[namafile,direktori]=uigetfile('*.txt','Load Data Magnet Format LEMI');
eval(['cd ''' direktori ''';']);
eval(['dataku=load(''' namafile ''')']);
[namafile2, direktori] = uiputfile('*.txt', 'Simpan Hasil Sebagai')
eval(['cd ''' direktori ''';']);
fout=fopen(namafile2,'w');
dataku = get(formku.figure1,'Userdata');
opts = detectImportOptions(dataku,"NumHeaderLines",12);
opts.SelectedVariableNames = ["DATE","TIME","ABKZ"];
T = readtable(dataku,opts);
DATE = T{:,1};
TIME = T{:,2};
TIME.Format = 'hh:mm:ss';
DOY = T{:,3};
d = T.DATE + T.TIME;
p = plot(d, DOY);
for i=1:length(DOY)
fprintf(fout,'%s %.2f\n', char(d(i)), DOY(i));
end
fclose(fout);
So, everyone... Please help me to do this work. Thank you very much... Im so grateful if you can solve my problem....
  9 Comments
Tyann Hardyn
Tyann Hardyn on 2 Jul 2021
I have not read the documentation, where is the link of it? and what the continuation after implementing the fullfile? please tell me how to connect the fullfile with the pushbutton i want.
Rik
Rik on 2 Jul 2021
If you have trouble with Matlab basics you may consider doing the Onramp tutorial.
You can either Google 'documentation fullfile matlab', or run this in Matlab:
doc fullfile

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!