Display Graph using checkbox

I have GUI with pushbutton and checkboxes, by pushbutton i am importing multiple excel files.Now i want to plot different graphs(from imported excel files) using different checkboxes,,if i click one check box it should display the graph and when uncheck graph should disappear. Please do the needful.

6 个评论

This is a very vague question. What have you tried so far and which problems occur? How could be suggest a matching code based on the screenshot of the GUI only?
Hello Jan, Sorry for late reply, by using below code i am able to plot only one plot, it is not working for multiple files, please help
if true
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)
[fileList, folder] = uigetfile('*.csv',
'Find the File to Import',
'Multiselect', 'on')
fileList = cellstr(fileList);
for k = 1:length(fileList);
baseFileName = fileList{k}
fullFileName = fullfile(folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
[numbers,txt,raw] = xlsread(fullFileName);
assignin('base', 'my_file', numbers)
assignin('base', 'a', numbers(6,:))
assignin('base', 'b', numbers(7,:))
assignin('base', 'c', numbers(8,:))
a = numbers(6,:)
b = numbers(7,:)
c = numbers(8,:)
d = numbers(10,:)
fullFileName = fullfile(folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
[numbers,txt,raw] = xlsread(fullFileName);
function checkbox1_Callback(hObject, eventdata, handles)
% hObject handle to checkbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
CheckBoxChecked(handles);
function CheckBoxChecked(handles)
if get(handles.checkbox1,'Value')
hold on
figure(1);
aa = evalin('base','a');
bb = evalin('base','b');
plot( aa,bb,'*--','DisplayName',baseFileName);
grid on;
end
function checkbox2_Callback(hObject, eventdata, handles)
% hObject handle to checkbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
CheckBoxChecked(handles);
function CheckBoxChecked(handles)
if get(handles.checkbox2,'Value')
hold on
figure(2);
aa = evalin('base','a');
cc = evalin('base','c')
plot( a,c,'*--','DisplayName',baseFileName);
grid on;
end
% Hint: get(hObject,'Value') returns toggle state of Torque
end
You need to define clearly what the expected behaviour is based on user actions. Technical problems like this regularly arise from simply not having considered exactly what is supposed to be the behaviour when buttons or checkboxes are clicked in various orders.
I'm still not clear what the order of user actions is here and what the expected responses are. You say it doesn't work for multiple files. So I assume every time the user clicks the button all the previous loaded data is expected to remain?
Adam thanks for reply, here first i am importing number of csv files by pushbutton callback, and storing them in workspace by assignin and retrieving the values in other callback(checkbox). But by above code it is only plotting graph for one data not for all imported files.
Well, the code in your pushbutton keeps overwriting the same variables so the previous data gets lost.
You really shouldn't be using things like assignin and evalin in a GUI. GUI functions have their own workspace and methods for sharing data amongst those workspaces, most simply by attaching the data to the handles structure. Pinging variables into the base workspace makes it that much harder to work with them.
If you want to retain data previously loaded though you will need to use arrays to store the data and append to those arrays when you load the next data rather than overwriting them. How you do that rather depends on the exact nature of the data you are storing. If it can be of variable size then use a cell array, if the data you will load into e.g. 'a' in your code will always be the same size then you can use a numeric array with dimensionality 1 higher than the data you currently store in 'a'.
Thanks Adam for valuable suggestion, but i am very new in Matlab GUI, i dont know about GUI workspace, if possible may you please provide some example. Thanks

请先登录,再进行评论。

回答(1 个)

Sandy - where is your data being drawn to? Does your GUI have an axes that you are plotting the data to or are you creating a new figure for each checkbox? The latter seems to be true with your calls to figure(1) and figure(2). Why not draw both to figure(1) or add an axes to your GUI so that all data is drawn directory to that axes?
Since you wish to remove the drawn data when the checkbox is unchecked, you will need to save the graphics object handle (for each drawn object) to your handles structure so that you can then delete it. For example,
function checkbox1_Callback(hObject, eventdata, handles)
if get(handles.checkbox1,'Value')
figure(1);
aa = evalin('base','a');
bb = evalin('base','b');
handles.hCbox1Plot = plot( aa,bb,'*--','DisplayName',baseFileName);
grid on;
hold on;
guidata(hObject,handles); % do this to save the updated handles structure
else
if ~isempty(handles.hCbox1Plot)
delete(handles.hCbox1Plot);
end
end
And as Adam commented, try not to pollute the workspace with your variables. Save them to the handles structure like we did in the above with the handle to the graphics object plotted in the checkbox1_Callback.

1 个评论

Thanks Geoff for your valuable suggestion, for single file import it is plotting the result after selecting checkbox but for multiple file import it is just overwriting the values. Also i have used guidata(hObject,handles) but still not getting the required results.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by