Display Graph using checkbox

8 views (last 30 days)
Sandy Baha
Sandy Baha on 31 Mar 2016
Commented: Sandy Baha on 18 Apr 2016
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 Comments
Adam
Adam on 15 Apr 2016
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'.
Sandy Baha
Sandy Baha on 15 Apr 2016
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

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 16 Apr 2016
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 Comment
Sandy Baha
Sandy Baha on 18 Apr 2016
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.

Sign in to comment.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!