필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Yvonne ting
Yvonne ting 2012년 5월 2일
편집: Alexei 2014년 11월 14일
% --- 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