Changing Plot Over Selection Button in a Buttongroup
2 views (last 30 days)
Show older comments
Hi, Community
Actually, my question is maybe a common / basic for a GUI MATLAB Programmer, because i just want to ask about how to change my Plotting Graph right after i click a (single) Button in my GroupButton Panel. So i have this Time Serise Graph in my GUI : ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/800234/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/800234/image.png)
And the code of that Graph is from this one (A Press Button Callback) :
% --- Executes on button press in loadplot.
function loadplot_Callback(hObject, eventdata, handles)
% hObject handle to loadplot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
........................ Stuffs
combowaktus_convs = vertcat(waktucombo_gabung{:}); %Time Series Data for X Plot
ff = get(handles.compmagz,'SelectedObject');
switch ff
case handles.xcomp
graph_ori_s = x_combi; %Magnetic Data for Y Plot
y_lbl_s = 'Variasi Medan Magnet Komponen X (Nanotesla / nT)';
case handles.ycomp
graph_ori_s = y_combi; %Magnetic Data for Y Plot
y_lbl_s = 'Variasi Medan Magnet Komponen Y (Nanotesla / nT)';
case handles.zcomp
graph_ori_s = z_combi; %Magnetic Data for Y Plot
y_lbl_s = 'Variasi Medan Magnet Vertical / Z (Nanotesla / nT)';
case handles.hcomp
graph_ori_s = hcalc_combi; %Magnetic Data for Y Plot
y_lbl_s = 'Variasi Medan Magnet Horizontal / H (Nanotesla / nT)';
case handles.fcomp
graph_ori_s = fcalc_combi; %Magnetic Data for Y Plot
y_lbl_s = 'Variasi Medan Magnet Total / F (Nanotesla / nT)';
otherwise
end
axes(handles.grafik);
plottings=plot(combowaktus_convs, graph_ori_s,'color', 'w','Parent',handles.grafik);
title(sprintf('Data Ekstraksi Magnet (%s) %s Stasiun %s', tipe_ekstrak1, mr_c, stasiun_ekstrak1), 'Color', 'y', 'fontsize', 11.5, 'fontweight','bold', 'Parent',handles.grafik);
xlabel(xlbl, 'Color', 'y','fontweight','bold','fontsize',10,'Parent',handles.grafik);
ylabel(y_lbl_s, 'Color', 'y','fontweight','bold','fontsize',10,'Parent',handles.grafik);
set(plottings,'LineWidth',1.1, 'Parent',handles.grafik);
set(handles.grafik,'Color',[1 0.96 0.9],...
'XGrid','on',...
'YGrid','on',...
'NextPlot','add');
storage = [combowaktus_convs(:) graph_ori_s(:)]; %% This is my Data Storage for X and Y Plot Data
set(handles.grafik, 'Userdata', storage); %% This is my Data Storage for X and Y Plot Data
And here is my RadioButton SelectionChange code :
function compmagz_SelectionChangedFcn(hObject, eventdata, handles)
% hObject handle to the selected object in compmagz
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selected_obj = hObject;
switch get(selected_obj, 'Tag')
case 'xcomp'
handles.value = 1;
answers = get(selected_obj, 'Value');
case 'ycomp'
handles.value = 2;
answers = get(selected_obj, 'Value');
case 'zcomp'
handles.value = 3;
answers = get(selected_obj, 'Value');
case 'hcomp'
handles.value = 4;
answers = get(selected_obj, 'Value');
case 'fcomp'
handles.value = 5;
answers = get(selected_obj, 'Value');
otherwise
handles.value = 0;
answers = 0;
end
I had created the Selection change Code without knowing how to handle the plot from my data storage. My question is, how to create a code to let me change the plot automatically right after i click one of Radio Button in my RadioButton Group? It should be from compmagz_SelectionChangedFcn function, right?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/800239/image.png)
At the above picture, i want to plot Z magnetic component right after i clicked Kom.Z Button, so in this case the graph would include X-plot as combowaktus_convs (Time series) and the Y-Plot as graph_ori_s (z_combi ) .
Noted : I had been attached my GUI files (m.file and fig.file) and also magnetic data for it.
So anyone, would you lend me a hand for the solution of my question.... Thank you very much, Everyone.... /.\ /.\ /.\
0 Comments
Answers (1)
Image Analyst
on 15 Nov 2021
I don't think this is the best way, but you'd do
function compmagz_SelectionChangedFcn(hObject, eventdata, handles)
% hObject handle to the selected object in compmagz
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selected_obj = hObject;
handles.xcomp = 0;
handles.ycomp = 0;
handles.zcomp = 0;
handles.hcomp = 0;
handles.fcomp = 0;
switch get(selected_obj, 'Tag')
case 'xcomp'
handles.xcomp = 1;
case 'ycomp'
handles.ycomp = 1;
case 'zcomp'
handles.zcomp = 1;
case 'hcomp'
handles.hcomp = 1;
case 'fcomp'
handles.fcomp = 1;
otherwise
handles.value = 0;
answers = 0;
end
% Now call the plotting function from inside the
% compmagz_SelectionChangedFcn() function
loadplot_Callback(hObject, eventdata, handles);
What I'd do instead is to just call the plotting function and have the checking of what radio button is selected done inside of that.
Like
% --- Executes on button press in loadplot.
function loadplot_Callback(hObject, eventdata, handles)
% hObject handle to loadplot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
........................ Stuffs
combowaktus_convs = vertcat(waktucombo_gabung{:}); %Time Series Data for X Plot
% Check the Value property of the individual radio buttons.
if handles.xcomp.Value
graph_ori_s = x_combi; %Magnetic Data for Y Plot
y_lbl_s = 'Variasi Medan Magnet Komponen X (Nanotesla / nT)';
elseif handles.ycomp.Value
graph_ori_s = y_combi; %Magnetic Data for Y Plot
y_lbl_s = 'Variasi Medan Magnet Komponen Y (Nanotesla / nT)';
elseif handles.zcomp.Value
graph_ori_s = z_combi; %Magnetic Data for Y Plot
y_lbl_s = 'Variasi Medan Magnet Vertical / Z (Nanotesla / nT)';
elseif handles.hcomp.Value
graph_ori_s = hcalc_combi; %Magnetic Data for Y Plot
y_lbl_s = 'Variasi Medan Magnet Horizontal / H (Nanotesla / nT)';
elseif handles.fcomp.Value
graph_ori_s = fcalc_combi; %Magnetic Data for Y Plot
y_lbl_s = 'Variasi Medan Magnet Total / F (Nanotesla / nT)';
else
end
axes(handles.grafik);
plottings=plot(combowaktus_convs, graph_ori_s,'color', 'w','Parent',handles.grafik);
title(sprintf('Data Ekstraksi Magnet (%s) %s Stasiun %s', tipe_ekstrak1, mr_c, stasiun_ekstrak1), 'Color', 'y', 'fontsize', 11.5, 'fontweight','bold', 'Parent',handles.grafik);
xlabel(xlbl, 'Color', 'y','fontweight','bold','fontsize',10,'Parent',handles.grafik);
ylabel(y_lbl_s, 'Color', 'y','fontweight','bold','fontsize',10,'Parent',handles.grafik);
set(plottings,'LineWidth',1.1, 'Parent',handles.grafik);
set(handles.grafik,'Color',[1 0.96 0.9],...
'XGrid','on',...
'YGrid','on',...
'NextPlot','add');
storage = [combowaktus_convs(:) graph_ori_s(:)]; %% This is my Data Storage for X and Y Plot Data
set(handles.grafik, 'Userdata', storage); %% This is my Data Storage for X and Y Plot Data
And the simplifed group selection would be simply one single line to call the loadplot_Callback() function.
function compmagz_SelectionChangedFcn(hObject, eventdata, handles)
% Now call the plotting function from inside the
% compmagz_SelectionChangedFcn() function
loadplot_Callback(hObject, eventdata, handles);
There is no need to store xcomp, ycomp, etc. as new, additional fields of handles. They're already there. Just check the "Value" property of the control at the time you actually need to check it.
11 Comments
Image Analyst
on 17 Nov 2021
Not sure what you want. Yes I load the listbox with all the files in the folder you selected. Then in the callback I automatically plot it. If you don't want anything to happen when you click an item in the listbox, you can get rid of the listbox call back function contents.
If you don't want a listbox and want the user to manually browse to it, you can do it like you were, though I think this is not very user friendly compared to just clicking on the filename in the listbox.
If you want the items to be listed in the listbox but don't want them to be automatically plotted, you can get rid of the plotting and have the user push a button.
What I often do is to have a checkbox that says "Auto-plot file" and then if checked, do the plot, but if unchecked, skip the plot.
Your step 2 is already done. Clicking on a filename automatically reads it in and plots it but like I said, you can change it to a more manual process if you want.
Your step 3 is already done. If you click a radio button, it updates the plot as soon as you click it.
Please explain in more detail exactly how you want it to work and upload the .m and .fig file (current/latest version).
See Also
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!