フィルターのクリア

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by