Output at GUI

4 次查看(过去 30 天)
Muhammad Taqi
Muhammad Taqi 2012-4-24
For my university project, i need to compile a programming for huffman coding algorithm. At the moment i have 2 main codes (.m files). one is GUI's m file and other is main program's m file. GUI's m file ask user input code and as soon as start button is pressed, the code is transferred to main program's m file where is completes whole task. The code for now is displaying output in matlab where as output is needed to be displayed in GUI. Can anybody please guide how to show the output in GUI. Following are the code.
%%%%%%%%---- GUI ----------%%%%%%%%%
function CODE_Callback(hObject, eventdata, handles)
data=get(hObject,'String');
input(data); %%transfer data to push button
% --- Executes on button press in START.
function START_Callback(hObject, eventdata, handles)
function input(data)
huff (data); %%calls main huff function
Main program code is
%%%%%%%----- MAIN PROGRAM --------%%%%%%%%
function huff(data) %%data from GUI
clc;
%fid=fopen('seq.txt','r');
seq5=data;
%seq=fread(fid,'*char');
%fclose(fid);
seq5=reshape(seq5,1,length(seq5));
testing(seq5);
[alpha prob]=probmodel(seq)
if ~isempty(alpha)
[huf entropy avglength redundancy]=huffman(alpha,prob);
if ~isempty(huf)
lp=length(prob);
for i=1:lp
str=huf(i).sym;
str=strcat(str,' :');
str=strcat(str,num2str(huf(i).prob));
str=strcat(str,' :');
str=strcat(str,huf(i).code);
disp(str);
end
disp(strcat('Entropy = ',num2str(entropy)));
disp(strcat('Average length = ',num2str(avglength)));
disp(strcat('Redundancy = ',num2str(redundancy)));
encseq=huffencode(huf,seq);
disp('Sequence :');
disp(seq);
disp('Encoded Sequence :');
disp(encseq);
decseq=huffdecode(huf,encseq);
disp('Decoded Sequence :');
disp(decseq);
end
else
display('Empty Sequence....');
end
end
Above program is working fine and displaying output in matlab. Only output to GUI file is needed. Also have created a static text in GUI and tagged with "OP" for displaying output.
Any help will be highly appreciated. Thanks in advance.

回答(2 个)

Jakob Sørensen
Jakob Sørensen 2012-4-24
You can call functions from you GUI just like you normally would, but with one exception. You need to pass the handles as well, since that's where the GUI stores everything. So without getting too specific, here is how you get data from your GUI, to and external function, and back again:
% Lets say you have a var called 'data' that needs to be proccessed:
handles.data = data;
% And you have a function called 'some_function'
handles.output = some_function(handles.data)
% Now show it in, lets say a static text
set(handles.staticTextName, 'String,' handles.output);
% And to update the handles
guidata(gcbo, handles);
(And I just realised that I made an example where the entire handles thing werent nessesary, but it's a good way to do stuff, if you want to make sure where your data is.)
  1 个评论
Muhammad Taqi
Muhammad Taqi 2012-4-24
Dear Jakob,
Thanks for your prompt response.
Highly appreciate it.
Actually, i am a newbie so little more help will be needed. Can you please help me with where exactly to amend above code and how to further continue.
I give my shot with above code and other techniques, but of no use till now :(

请先登录,再进行评论。


Jakob Sørensen
Jakob Sørensen 2012-4-27
In that case, you need to clarify something for me, since I can't see your GUI...
  1. How exactly does you GUI work? Is it a text field input and a submit button, or how?
  2. What do you want the external function to do? Does it calculate something and return it to the GUI? Or does it plot it right away?
  3. What input and output do you have from the external function.

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by