This is the Fourth question of a serie of questions to help me complete a project. You can see the Third question here .
This is at the moment the Last Question. (I will update it if I'll ask something else).
You can see the gui in the attached file .png and test it by opening the compressed archive. The code is commented enough (I hope) to easily understand what I am doing.
I am trying to save the plots to handles.plots every time that PB2 is pressed (Plot!). If the checkbox was already checked on a previous interaction with PB2, the plot is already in the handles, hence it doesn't re-do all the actions taken before.
The code works properly except for one detail, let's say we press the checkboxes 1 and 3. If we enter debug mode, the plot saved after the first loop (i=1) is accessible as get(handles.plots{1}). For i = 2 nothing happens and for i = 3 (True checkbox) it saves the new plot to the handles.plots{3}, which becomes accessible as get(handles.plots{3}). The problem is that the plot saved in handles.plots{1} is deleted in the process without a logical reason (for me at least).
Why am I doing this? Because I want to find a way to assign the color to a plot, in a way that if I uncheck it and re-checks it it will reappears with the same properties on the graph. Right now I got to a point were each time you press PB2, each graph changes color, even if it was already plotted.
Don't forget that the code is attached if you want to see the entire code. I will be enourmosly greatful if you'd take 5 mins to check it.
Here is the code for PB2. Tell me if I can improve my code by vectorizing some of the loops in PB2.
% --- Executes on button press in PB2.
function PB2_Callback(hObject, eventdata, handles)% Recover the handles
handles = guidata(hObject);files = handles.files;X = handles.plots;% Save in table the logical value of the checkboxes and convert to mat
table = get(handles.uitable2, 'Data');CB_answer = cell2mat(table(1:end,1));% clear the plot and make it invisible
cla;set(handles.axes1, 'Visible', 'off')% This will be skipped the first time we press PB2. If we have already
% pressed it, in handles.plots there will be stored some plots,
% we have to convert them to 1 to use the next part of the code
for z = 1:length(X) if find(X{z} ~= 0) X{z} = 1; endend% Convert X to mat to further use
X1 = cell2mat(X);% The first time we press the button PB2, X1 will be empty, hence we
% preallocate the memory by creatintg an empty cell of the right size
if all(X1(:)==0) handles.plots(1:length(files)) = {0}; X = handles.plots; X1 = cell2mat(X);end% Check the status of the pushbutton, if they are true their value is == 1
for i = 1:length(CB_answer) if CB_answer(i) == 1 % If the value of X1(i) == 0 it could mean that we pressed PB2 for
% the first time or the last time we pressed it, the checkbox(i)
% wasn't selected, hence it's not saved in handles.plots{i}.
if X1(i) == 0 f1 = cell2mat(files{i,1}); f2 = cell2mat(files{i,2}); handles.plots{i} = plot(f1,f2); end end endset(handles.axes1, 'Visible', 'on')guidata(hObject, handles);end
Best Answer