How to handle radiobutton in radiobutton group panel...

8 views (last 30 days)
Hi, Community....
I have a question about radiobutton group.
<<< ----- This is my ButtonGroup
So i have tried with this code to handle a plot of the selected component based on radiobutton clicked in my radiobutton group panel :
function matikan_radio_button(off)
set(off, 'Value', 0)
% --- Executes on button press in kompx.
function kompx_Callback(hObject, eventdata, handles)
% hObject handle to kompx (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of kompx
off = [handles.komph, handles.kompz, handles.kompf, handles.kompy];
matikan_radio_button(off)
pilih = 1;
pilih = handles.pilih;
guidata(hObject, handles);
% --- Executes on button press in kompy.
function kompy_Callback(hObject, eventdata, handles)
% hObject handle to kompy (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of kompy
off = [handles.komph, handles.kompz, handles.kompx, handles.kompf];
matikan_radio_button(off);
pilih = 2;
pilih = handles.pilih;
guidata(hObject, handles);
% --- Executes on button press in kompz.
function kompz_Callback(hObject, eventdata, handles)
% hObject handle to kompz (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of kompz
off = [handles.komph, handles.kompf, handles.kompx, handles.kompy];
matikan_radio_button(off);
pilih = 3;
pilih = handles.pilih;
guidata(hObject, handles);
% --- Executes on button press in komph.
function komph_Callback(hObject, eventdata, handles)
% hObject handle to komph (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of komph
off = [handles.kompf, handles.kompz, handles.kompx, handles.kompy];
matikan_radio_button(off);
pilih = 4;
pilih = handles.pilih;
guidata(hObject, handles);
% --- Executes on button press in kompf.
function kompf_Callback(hObject, eventdata, handles)
% hObject handle to kompf (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of kompf
off = [handles.komph, handles.kompz, handles.kompx, handles.kompy];
matikan_radio_button(off);
pilih = 5;
pilih = handles.pilih;
guidata(hObject, handles);
function pushbutton1_Callback(~, ~, 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)
[namafile,direktori]=uigetfile({'*.txt', 'Text-files (*.txt)';'*.sec', 'Seconds-Files (*.sec)';'*.min', 'Minutes-Files (*.min)'},'Load Data Magnet LEMI Non RAW Non IAGA (Menitan / Detikan)', 'Multiselect','on');
cd(direktori);
full = fullfile(direktori,namafile);
namafiles = cellstr(sort(namafile)); %Mensortir Kombinasi Data Yang Dipilih secara acak (PENTING)
nfiles = size(full,2);
f = cellstr(full);
file = length(f);
axes(handles.satu);
if file == 1
[~, base, ~] = fileparts(full);
lastIndex = length(base);
% Capitalize first character and lower case remaining characters.
nb = [upper(base(1)), lower(base(2:lastIndex))];
sta = extractBefore(nb,"_lemi");
opts = detectImportOptions(full,'ReadVariableNames',false, 'Delimiter', ' ', 'EmptyFieldRule', 'missing', 'EmptyLineRule', 'skip',...
'Whitespace', '', 'FileType','text',...
'TreatAsMissing',{'99999.00', '99.00', '99', '0.00','00000.00', '00.00', '0.000000', '0', 'N/A'}, 'ConsecutiveDelimitersRule', 'join');
B = readmatrix(full, opts);
tahun = B(1:end,1);%: Mendapatkan data tahun pada kolom 1
bulan = B(1:end,2);%: Mendapatkan data bulan pada kolom 2
tanggal = B(1:end,3);%: Mendapatkan data tanggal pada kolom 3
jam = B(1:end,4);%: Mendapatkan data jam pada kolom 4
menit = B(1:end,5);%: Mendapatkan data menit pada kolom 4
detik = B(1:end,6);%: Mendapatkan data detik pada kolom 6
x = B(1:end,7);%: Mendapatkan data magnet arah sumbu x pada kolom 7
y = B(1:end,8);%: Mendapatkan data magnet arah sumbu y pada kolom 8
z = B(1:end,9);%: Mendapatkan data magnet arah sumbu z pada kolom 9
DOF = B(1:end,13);%: Mendapatkan data medan magnet Komponen F pada kolom 13
D = datetime(tahun,bulan,tanggal,0,0,0,'TimeZone','Asia/Jakarta');
DOH = sqrt((x.^2)+(y.^2)); %Kalkulasi Medan Magnet Horizontal H
DOF = sqrt((x.^2)+(y.^2)+(z.^2)); %Kalkulasi Medan Magnet Total F
waktu = string(D(1,1));
time = hours(jam) + minutes(menit) + seconds(detik);
time.Format = 'hh:mm:ss';
dur = D + time;
type = seconds(duration(jam(2), menit(2), detik(2)) - duration(jam(1), menit(1), detik(1)));
switch type
case 1
mr_v = "(1s)";
case 60
mr_v = "(1m)";
case 3600
mr_v = "(1h)";
otherwise
% handle unauthorised values
end
end
pilihan = get(handles.pilih);
switch pilihan
case 1 %This should to handle kompx radio button tag name
flemx = figure('Name','Magnetic Field North-South(X) Variation Versus Time Series');
flemx.WindowState='maximized';
plot(dur, x, 'Color', 'g', 'LineWidth',1.1);
title(sprintf('Variasi Medan Magnet North-South(X) Vs Waktu Data LEMI %s Periode %s Stasiun %s',mr_v, waktu, sta), 'fontsize', 13, 'fontweight','bold');
xlabel('Deret Waktu (Time Series)','fontweight','bold','fontsize',10);
ylabel('Variasi Medan Magnet North-South (Nanotesla / nT)','fontweight','bold','fontsize',10);
legend('Kurva Komponen Magnet North-South(X)', 'Parent',flemx);
grid on
axes(handles.satu);
clemisingle_x = plot(dur, x,'Color','c','Parent',handles.satu);
title(sprintf('Variasi Medan Magnet North-South(X) Vs Waktu Data LEMI %s Periode %s Stasiun %s',mr_v, waktu, sta), 'fontsize', 12, 'fontweight','bold','Parent',handles.satu);
xlabel('Deret Waktu (Time Series)','fontweight','bold','fontsize',10,'Parent',handles.satu);
ylabel('Variasi Medan Magnet North-South (Nanotesla / nT)','fontweight','bold','fontsize',10,'Parent',handles.satu);
grid on
set(gca,'color',[0.3 0.3 0.3],'Parent',handles.satu);
set(clemisingle_x.figure1,'CurrentAxes',handles.satu ,'Parent',handles.satu);
set(clemisingle_x,'LineWidth',1,'Parent',handles.satu);
set(handles.satu,'Color',[0.9 0.4 0.4],...
'XGrid','on',...
'YGrid','on',...
'NextPlot','add', 'Parent',handles.satu);
set(handles.figure1,'Userdata',namafile);
delete(wait)
case 2 %This should to handle kompy radio button tag name
.....
end
end
I tried with that code, but my the axes didnt do plotting based on my selected radio button in radio button group..... For example, if i choose radiobutton with kompx tag name , then this should handle
switch pilihan
case 1....
and plot based on the data x versus time at the code. However it doesnt work.
So, everyone, would you lend me a hand to solve this problem? Iam very grateful if someone can solve my problem here....
Thank you very much everyone... /.\ /.\ /.\

Answers (1)

Geoff Hayes
Geoff Hayes on 1 Oct 2021
Tyann - I think the assignments in each radio button callback is incorrect
pilih = 3;
pilih = handles.pilih;
guidata(hObject, handles);
Don't you want to assign as
handles.pilih = 3;
guidata(hObject, handles);
instead? Or do you even need this? If you just want to know which radio button is enabled, then update your switch statement from
switch pilihan
case 1 %This should to handle kompx radio button tag name
to an if/elseif as
if get(handles.kompx, 'Value')
% stuff
elseif get(handles.kompy, 'Value')
% etc.
end
Since your radio buttons are within a radio button group, then only one will be enabled at any one time. And you know if it is enabled because Value is 1.
  6 Comments
Tyann Hardyn
Tyann Hardyn on 3 Oct 2021
Edited: Tyann Hardyn on 3 Oct 2021
Do you want to dynamically update the plot as the user chooses a new radio button?
Correct, SIr. Yes, that what i meant. When i press a button (Call Data Button) to get a data (lets say Magnet Data), it will plot as A versus Time because the RadioButtonA was selected before, then, if i select another RadioButton, i.e RadioButtonB, so it would plot B versus Time automatically, where A and B is a data series in Magnet Data which re get by pressing Call Data Button before
Could it possible, Sir? Thankyou very much /.\
Image Analyst
Image Analyst on 3 Oct 2021
@Tyann Hardyn, please attach the .fig and .m files with the paperclip icon if you need anymore help.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!