Plotting Data by Using Loaded TXT File and Gui Pushbutton Problem

Dear, everyone. I have a problem with my Gui Pushbutton Script to plt my loaded txt data here... This is my script :
function pushbutton11_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton11 (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 RAW','Multiselect','on');
eval(['cd ''' direktori ''';']);
eval(['mydata=load(''' namafile ''')']);
tahun = mydata(:,1); %: Getting data in column 1
bulan = mydata(:,2); %: Getting data in column 2
tanggal = mydata(:,3); %: Getting data in column 3
jam = mydata(:,4); %: Getting data in column 4
menit = mydata(:,5); %: Getting data in column 5
detik = mydata(:,6); %: Getting data in column 6
kompx = mydata(:,7); %: Getting data in column 7
kompy = mydata(:,8); %: Getting data in column 8
kompz = mydata(:,9); %: Getting data in column 9
w = seconds((3600*jam)+(60*menit)+ detik);
w.Format = 'hh:mm';
D = datetime(tahun,bulan,tanggal,jam,menit,detik,'TimeZone','Asia/Jakarta');
s = (w/86400)+tanggal;
for i=1:length(detik)
hmagnet = sqrt((kompx.^2)+(kompy.^2));
fmagnet = sqrt((kompx.^2)+(kompy.^2)+(kompz.^2));
end
p = plot(D, hmagnet);
xlabel('Time Series','fontweight','bold','fontsize',10);
ylabel('Horizontal Magnetic Component Of Non IAGA Lemi Format (nT)','fontweight','bold','fontsize',10);
legend('off');
set(formku.figure1,'CurrentAxes',formku.satu);
set(p,'LineWidth',1);
set(formku.satu,'Color',[1 0.96 0.9],...
'XGrid','on',...
'YGrid','on',...
'NextPlot','add');
set(formku.figure1,'Userdata',mydata);
grid on
I just want to get the desired column data to be calculated by using Pythagoras formula as hmagnet or fmagnet data column and then plotting the result versus TIME data in D variable on my GUI .fig program but it never happened.
It just show me as a blank cartesian default figure.... So everyone, im so grateful if you help my problem here... Thank you very much...

10 件のコメント

Rik
Rik 2021 年 7 月 6 日
As you have been asked in your previous question:
  1. Why are you using eval and cd? You need to use fullfile to compose the input for load.
  2. Have you tried using the debugger do step through your code line by line?
There are several steps to what you want to do. First the reading of your file, then the parsing of those contents to the variables you want to plot, then the plotting itself. Which of these steps is causing the issue?
I would also suggest using English comments to explain your code. That will make it easier for people on this forum. The overwhelming majority can read/write English, but only few visitors will be able to read Indonesian without Google Translate.
Tyann Hardyn
Tyann Hardyn 2021 年 7 月 6 日
編集済み: Tyann Hardyn 2021 年 7 月 6 日
@Rik alright, im sorry for using eval and cd again, but im just thinking that using eval will become success like my other previous script (without fullfile)... also, i have been changed my Indonesian languange to English in comment
Rik
Rik 2021 年 7 月 6 日
[namafile,direktori]=uigetfile('*.txt','Load Data Magnet LEMI RAW','Multiselect','on');
mydata=load(fullfile(direktori,namafile));
Note that the user could select multiple files, resulting in an error when using load.
So now we have removed cd and load. Did you set a breakpoint to step through your code?
Another suggestion:
p = plot(D, hmagnet,'Parent',formku.handle_to_your_axes);
You should do this for every to any graphics object.
And why are you storing the array to the UserData, instead of the guidata struct?
Tyann Hardyn
Tyann Hardyn 2021 年 7 月 7 日
I didnt get whats the variable of "handle_to_your_axes" in my Gui script represented of.
Rik
Rik 2021 年 7 月 7 日
The handle to your axes object. You seem to be using GUIDE (even though that's a bad idea), so it is probably called formku.axes1
Tyann Hardyn
Tyann Hardyn 2021 年 7 月 7 日
編集済み: Tyann Hardyn 2021 年 7 月 7 日
@Rik yes indeed, you re right... Im using guide to sketch Gui properties directly and then use its tag to create their script... Im using only 1 axes called satu to handle graph from three Gui pushbutton, but its still didnt work by using formku.satu.... It can only handle 1 Gui pushbuttons sucessfully and here is the script :
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (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','Multiselect','on');
eval(['cd ''' direktori ''';']);
eval(['dataku=load(''' namafile ''')']);
tahun = dataku(:,1); %: Getting data in column 1
bulan = dataku(:,2); %: Getting data in column 2
tanggal = dataku(:,3); %: MGetting data in column 3
jam = dataku(:,4); %: Getting data in column 4
menit = dataku(:,5); %: Getting data in column 5
detik = dataku(:,6); %: Getting data in column 6
xmagnet = dataku(:,7); %: Getting data in column 7
ymagnet = dataku(:,8); %: Getting data in column 8
zmagnet = dataku(:,9); %: Getting data in column 9
h = dataku(:,10); %: Getting data in column 10
total = dataku(:,13); %: Getting data in column 11
w = seconds((3600*jam)+(60*menit)+ detik);
w.Format = 'hh:mm';
D = datetime(tahun,bulan,tanggal,jam,menit,detik,'TimeZone','Asia/Jakarta');
s = (w/86400)+tanggal;
xi =min(w):3600:max(w);
p = plot(D, h);
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(p,'LineWidth',1);
set(formku.satu,'Color',[1 0.96 0.9],...
'XGrid','on',...
'YGrid','on',...
'NextPlot','add');
set(formku.figure1,'Userdata',dataku);
grid on
By using that gui pushbutton 1, the plot can be done in formku.satu axes but the remains 2 (one of them are shown in my question / pushbutton11) re still didnt work
Rik
Rik 2021 年 7 月 7 日
You're making it hard to help you. Why haven't you applied my suggested edit?
GUIDE will automatically generate handles. Why don't you use those instead? And why did you not edit the call to plot?
And did you already try using breakpoints to step through the code line by line?
Tyann Hardyn
Tyann Hardyn 2021 年 7 月 8 日
編集済み: Tyann Hardyn 2021 年 7 月 8 日
@Rik Omg its worked finally, its so great. i ve changed my question code to become like these as your suggestion, Rik :
function pushbutton11_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(gcbo);
[namafile,direktori]=uigetfile('*.txt','Load T Magnet LEMI RAW');
full = fullfile(direktori, namafile);
[~, base, extension] = fileparts(full);
lastIndex = length(base);
% Capitalize first character and lower case remaining characters.
nb = [upper(base(1)), lower(base(2:lastIndex))];
pit = extractAfter (nb,"lemi_raw_");
sta = extractBefore(nb,"_lemi_raw");
th = "Hasil Plotting Komponen Magnet H Data" + " " + sta + " " + "(" +pit + ")" ;
tf = "Hasil Plotting Komponen Magnet F (Total) Data" + " " + sta + " " + "(" +pit + ")" ;
opts = delimitedTextImportOptions("NumVariables", 21);
% Specify range and delimiter
opts.Delimiter = " ";
% Specify column names and types
opts.VariableNames = ["Var1", "Var2", "Var3", "Var4", "Var5", "Var6", "Var7", "Var8", "Var9", "Var10", "Var11", "A", "N", "FL", "Var15", "Var16", "Var17", "Var18", "Var19", "Var20", "Var21"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "categorical", "double", "double", "double", "string", "string", "string", "string", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";
T = readtable(full, opts);
tahun = mydata(:,1); %: Getting data in column 1
bulan = mydata(:,2); %: Getting data in column 2
tanggal = mydata(:,3); %: Getting data in column 3
jam = mydata(:,4); %: Getting data in column 4
menit = mydata(:,5); %: Getting data in column 5
detik = mydata(:,6); %: Getting data in column 6
kompx = mydata(:,7); %: Getting data in column 7
kompy = mydata(:,8); %: Getting data in column 8
kompz = mydata(:,9); %: Getting data in column 9
w = seconds((3600*jam)+(60*menit)+ detik);
w.Format = 'hh:mm';
D = datetime(tahun,bulan,tanggal,jam,menit,detik,'TimeZone','Asia/Jakarta');
for i=1:length(detik)
hmagnet = sqrt((kompx.^2)+(kompy.^2));
fmagnet = sqrt((kompx.^2)+(kompy.^2)+(kompz.^2));
end
kurva = plot(D, hmagnet,'Parent',handles.satu);
xlabel('Deret Waktu (Time Series)','fontweight','bold','fontsize',10);
ylabel('Komponen H Magnet Data Lemi RAW Non IAGA Format (nT)','fontweight','bold','fontsize',10);
legend('off');
axes(handles.satu);
set(handles.figure1,'CurrentAxes',handles.satu);
set(kurva,'LineWidth',1,'Parent',satu);
set(handles.satu,'Color',[1 0.96 0.9],...
'XGrid','on',...
'YGrid','on',...
'NextPlot','add');
set(handles.figure1,'Userdata',namafile);
changing " formku = guidata(gcbo); " to become : " handles = guidata(gcbo); " and adding :
axes(handles.satu);
and also changing from kurva = plot(D, hmagnet); to become :
kurva = plot(D, hmagnet,'Parent',handles.satu);
Solved, thank you very much
And, how did you know im come from Indonesian and guess my language as bahasa??
Tyann Hardyn
Tyann Hardyn 2021 年 7 月 8 日
@Rik Why you dont answer my question in the "answer this question " column so that i can give you accepted anwer mark?
Rik
Rik 2021 年 7 月 8 日
One minor addition: the handles struct is already loaded, so your first line is not required.
I guessed your language by pasting your comments in Google translate.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

2021 年 7 月 6 日

コメント済み:

Rik
2021 年 7 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by