Chris Damage
Chris Damage

Reputation: 13

MATLAB: Pushbutton callbacks to update and amend variables and cell arrays

I am new to making GUIs and this is my first attempt, but I have spent days searching for an answer to a really basic pushbutton question.There was a similar question on here but it doesn't work for me as I need to make multiple changes with a single click.

The GUI consists of a table which lists a number of images along with two blank columbs (the final GUI will have a 1000,50 table). The GUI displays the first image and then has 3 buttons,buttons 1 & 2 insert data into the table and then displays the next image,updating the Row variable within the code so you can move down the list of images, with each button press. Button 3 is different though, it takes the data from the current row and places it in a separate table for rejected data, and then once again updates the row variable. However, I can't get my callback functions to update the Row,Table and Rejected_Data variables. My code is as follows:

%Simple GUI for to show issue
%Table
Table=cell(4,3);
Table(1,1:end)=[{'File'},{'Type'},{'Value'}];
Table(2,1)={'Image1.jpg'};
Table(3,1)={'Image2.jpg'};
Table(4,1)={'Image3.jpg'};
% Rejected Images
Rejected_Data=cell(4,3);
Rejected_Data(1,1:end)=[{'File'} {'Type'} {'Value'}];

% GUI bit

Row=2;
Im=imread(Table{Row,1});
%Figure
hFig=figure;
set(hFig,'Units','Normalized','Position',[0.1 0.1 0.6 0.6]);
%Axes
hAx=axes('Parent',hFig);
set(hAx,'Units','Normalized','Position',[0.2 0.4 0.5 0.5]);
Image=imagesc(Im,'Parent',hAx);
axis off;
%button1
bh1=uicontrol('Style','pushbutton','String','Button1');
set(bh1,'Units','Normalized','Position',[0.2 0.2 0.1 0.05]);
%button2
bh2=uicontrol('Style','pushbutton','String','Button2');
set(bh2,'Units','Normalized','Position',[0.4 0.2 0.1 0.05]);
%button3
bh3=uicontrol('Style','pushbutton','String','Scrap');
set(bh3,'Units','Normalized','Position',[0.6 0.2 0.1 0.05]);

% Callbacks
set(bh1,'Callback',{@buttontest1,Table,Row,hAx});
set(bh2,'Callback',{@buttontest2,Table,Row,hAx});
set(bh3,'Callback',{@buttontest3,Table,Row,hAx,Rejected_Data});

Function for Buttons 1 & 2 is:

function buttontest1(~,~,Table,Row,hAx)
Table(Row,2)=cellstr('Tree');
Table(Row,3)=47;
Row1=Row+1;
evalin('base','Row = Row1');
evalin('base','Table = Table');
Pic=imread(Table{Row1,1});
imagesc(Pic,'Parent',hAx);
end

Function for Button 3:

function buttontest3(~,~,Table,Row,hAx,Rejected_Data)
Rejected_Data(Row,:)=Table(Row,:);
Row1=Row+1;
evalin('base','Row = Row1')
Pic=imread(Table{Row1,1});
imagesc(Pic,'Parent',hAx);
end

I tried using global instead of evalin, but as Row etc, already exists it doesn't work, I have also tried assignin, but I couldn't get that to work either. I don't want to use guide as I need the flexibility of only having a text file, I also don't use nested functions as they require my original GUI to be a function also and the final GUI will we bolted onto some pre-processing that will create the data table inputted.

Any help would be greatly appreciate. Thanks

Upvotes: 0

Views: 1085

Answers (2)

kAmol
kAmol

Reputation: 1389

Use handles structure to handle data transfer in gui.

Upvotes: 0

Joshua Barr
Joshua Barr

Reputation: 396

Use guidata to store your data. Using evalin for this purpose is not recommended.

Upvotes: 1

Related Questions