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

2 次查看(过去 30 天)
% --- 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.

回答(2 个)

TAB
TAB 2012-5-3
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 个评论
mohammad asasi
mohammad asasi 2012-5-7
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.

请先登录,再进行评论。


Jan
Jan 2012-5-3
Instead of using fragile global variables, storing the data using guidata or setappdata / getappdata locally in the figure is easier to debug.
  3 个评论
Sean de Wolski
Sean de Wolski 2012-5-7
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 2014-11-14
编辑:Alexei 2014-11-14
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by