Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
% --- 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.

Risposte (2)

TAB
TAB il 3 Mag 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 Commento
mohammad asasi
mohammad asasi il 7 Mag 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.

Accedi per commentare.


Jan
Jan il 3 Mag 2012
Instead of using fragile global variables, storing the data using guidata or setappdata / getappdata locally in the figure is easier to debug.
  3 Commenti
Sean de Wolski
Sean de Wolski il 7 Mag 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 il 14 Nov 2014
Modificato: Alexei il 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.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by