edit text, push button and output.

Dear All, while working for my university project, am bit stuck in the final steps of GUI. Have compiled a code for Huffman coding. what my requirement is to get input from user which i am able to by creating GUI and placing edit text. Also am able to get entered text.
f unction CODE_Callback(hObject, eventdata, handles)
% hObject handle to CODE (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
data=get(hObject,'String');
% Hints: get(hObject,'String') returns contents of CODE as text
% str2double(get(hObject,'String')) returns contents of CODE as a double
Now i need help to communicate entered text (variable named as "data") with start button to run the complete program. and also to show final output on GUI (text based output)
% --- Executes on button press in START.
function START_Callback(hObject, eventdata, handles)
% hObject handle to START (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
huff (data); % calling main program for huffman coding
-----%%%%%%-----------
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);
(Want to display all output on GUI)
Urgent help will be highly appreciated. Thanks in advance.

1 Comment

Wholla,,,, I managed to figure out my first query, did managed to make start button work.
Help regarding output is required.
thanks :)

Sign in to comment.

Answers (2)

If you make an additional text box on your gui you can then set the string of that text box to be str where
str = {
strcat('Entropy = ',num2str(entropy))
strcat('Average length = ',num2str(avglength))
strcat('Redundancy = ',num2str(redundancy))
...
}

7 Comments

Thanks for your reply.
Actually, the disp command is running under loop and also its on separate function.
Will above command will transfer data to GUI function??
"
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
"
I want to show all these outputs on same GUI where start button is located.
You need to make the text box and set the string of the text box.
yes.
it could be any text box (edit or static text) where all above outputs can be displayed.
What i tried is to create a new function in GUI part and transfer the data from original function to GUI.
%% IN ORIGNAL HUFFMAN CODE
testing(seq5); %% seq5 is string of char taken from user
%% IN GUI
function testing(seq5)
seq=seq5;
[alpha prob]=probmodel(seq)
....
so that all loops can run directly on GUI and can display the desired output.
But for above code, its prompting me an error.
"??? Undefined function or method 'testing' for input
arguments of type 'char'."
what am i doing wrong here?
That wasn't a question, it was telling you what you need to do in general terms.
So what should i do for the new function to work??
You need to read about how to create functions and guis in MATLAB.

Sign in to comment.

Don't use disp() because that outputs to the command window. If you want it to go to your GUI, use set(). Lets say you have a static text control called txtInfo. Then you can use this code:
% Create string for output that we can send to the text control.
txtInfo = sprintf('Entropy = %.4f\nAverage length = %.4f\nRedundancy = %.4f', entropy, avglength, redundancy);
% Now, send the string to the control called txtInfo
set(handles.txtInfo, 'String', txtInfo);
If that is in a tight loop, you might also need to add a "drawnow" to get it to update immediately.

4 Comments

Thanks @image analyst for your reply.
Am at novice level of matlab programming and little more help will be highly appreciated.
At the moment, i have 2 .m files, one for gui & other for main program.
the loop and all disp commands are in main program m file, where as the output to be displayed is in gui for which i believe above commands must be in gui's m file. So how to transfer that loop to gui's m file.
Below mentioned code is my main program code.
how will i shift to GUI's m file.
function huff(data) %% data is value taken from user 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
Can't you just copy and paste it from one file into the next, at the appropriate place where you want it to be executed? Like in the callback of a button? Maybe I'm missing something.
Well, if i replace disp() with set(), its still giving an error.
??? Undefined variable "handles" or class "handles.txtInfo".
Also, i tried creating an output function as
txtInfo = sprintf('Entropy = %.4f\nAverage length = %.4f\nRedundancy = %.4f', entropy, avglength, redundancy);
then i want to transfer that string to my GUI function for display.
How should i sent there?
Have named my GUI file as TEST2.
function varargout = TEST2(varargin)

Sign in to comment.

Categories

Asked:

on 23 Apr 2012

Community Treasure Hunt

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

Start Hunting!