HELP! passing variable from one callback to another callback in GUI

5 views (last 30 days)
% --- Executes on button press in Embedded.
function Embedded_Callback(hObject, eventdata, handles)
% hObject handle to Embedded (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global msglen
global filesize
global jobj2
%image_temporary= A;
jobj=jpeg_read (get(handles.imagename,'string'));
msgstr = get(handles.EmbeddedMsg,'string');
if isempty(get(handles.EmbeddedMsg,'string')) % User tried to encode nothing!
fprintf('\n\t\t No message entered, try again.\n\n')
close(gcbf)
return
else
msgdbl = double(msgstr);%decimal the string
display(msgdbl)
msgbin = de2bi(msgdbl,8);
% display(msgbin)
msgbincv = reshape(msgbin,18*8, 1);
% display(msgbincv )
msg = msgbincv;
% display(msg )
msglen = length(msgbincv);
display(msglen)
key = sum(100*clock);
step = 2;
jobj2 = jpeg_steg_encode(jobj,msg,key,step);
jobj2.optimize_coding = 1; % optimize huffman tables (optional)
jpeg_write(jobj2,'output.jpg');
d=dir('output.jpg');
filesize = d.bytes;
display(filesize)
axes(handles.stego_image);
imshow('output.jpg',handles.stego_image);
setappdata(h,'name',jobj2)
fprintf('\t\t\n Your message was encoded.\n\n');
end
% --- Executes on button press in retrieve.
function retrieve_Callback(hObject, eventdata, handles)
% hObject handle to retrieve (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Decode the message, and verify that it matches the original message.
msg2 = jpeg_steg_decode(jobj2,key,step);
msgrbin = vec2mat(msg2,18);
msgrbin(9:end,:) = [];
msgtdbl = transpose(msgrbin);
% display(msgtdbl)
msgrdbl = bi2de(msgtdbl);
% display(msgrdbl)
msgt = transpose(msgrdbl);
% display(msgt )
msgr = strcat(msgt);
% display(msgr )
mlen = min(length(msg),length(msg2));
capacity = 100*mlen/(8*filesize);
if isequal(msg(1:mlen),msg2(1:mlen))
fprintf('PASSED: Message of %d bits successfully decoded', mlen);
fprintf(' (capacity = %.2f%%)\n', capacity);
else
bitscorrect = length(find(msg(1:mlen)==msg2(1:mlen)));
fprintf('FAILED: %d bit(s) incorrect\n', mlen-bitscorrect);
end
set(handles.message,'string',msgr)
I have problem when i'm passing the jobj2 from the embedded callback to retrive callback? i'm using global for jobj2 and key, step and so on.
But get error of when i used global: Attempt to reference field of non-structure array.

Answers (2)

TAB
TAB on 3 May 2012
Global variables need to be declare global in each function where it is used.
Add below declartion line in your retrieve_Callback() function also.
global jobj2;
  1 Comment
mohammad asasi
mohammad asasi on 7 May 2012
thanks you helped me with global...
I used to define a variable global only one time in the first call back...and it did,nt work.

Sign in to comment.


Jan
Jan on 3 May 2012
Instead of using fragile global variables, storing the data using guidata or setappdata / getappdata locally in the figure is easier to debug.
  3 Comments
Sean de Wolski
Sean de Wolski on 7 May 2012
Yes that looks good, Yvonne!
More info and examples:
http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F
Alexei
Alexei on 14 Nov 2014
Edited: Alexei on 14 Nov 2014
Sean de Wolski, that link you provided is FANTASTIC! Thank you! Only thing is it does not take you to the subsection directly -- just to the top of the page. But I got to where I wanted to be via ctrl+f so it's all good.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!