How to return loop result (string) from an mfile to another mfile(editText GUI)?

I have two mfiles. One mfile (gui.m) is gui with buttons (for browse and result) and edittext component. another mfile (function.m) with looping function. when i run function.m, i want to return the result to edittext in gui.m
in function.m:
function a = function (f)
i=f; %i is browsed image from gui.m
%this loop result that i want to call to edittext
w=[];
k=0;
for ...
w=[a b];
k=k+1;
end
end
a=w; %to return the result a as w
in gui.m:
function resultButton_Callback(hObject, eventdata, handles)
set(handles.edit5);
covImg= handles.covImg; %covImg is browsed image
G = function(covImg); %call function.m
guidata(hObject,handles);
handles.covImg = G;
imshow(handles.covImg);
looking forward for any response. thanks in advance.

4 Comments

Ria - please clarify what you want to show in the edit text box. The result of function appears to be an array (or image) which you save to the covImg field of the handles structure which you then display using imshow. What then do you want to display in the edit text box?
I realize that you have probably only posted pseudo code in your question, but you should name your function appropriately so that the reader can get an idea of what it is doing and what it will return.
Hayes - Thanks for reviewing my question.
I want to show the array of the looping result from function.m to edittext in gui.m i noticed that i don't have to use imshow(handles.covImg) but i still can't manage to set the result (array) to edittext
function a = identify (f)
I=(f);
Ib=I;
% I=~I;
[bar,kol]=size(I);
% horisontal projeksi
Y=sum(I,2);
koY=1:bar;
% plot(Y,1:bar);
% hold off;
% pemotongan untuk baris
line1=zeros(1,bar);
line2=zeros(1,kol);
k1=length(Y);
k=1;
for ii=2:k1-1
if Y(ii)==0
if Y(ii+1)~=0
line1(k)=koY(ii+1);
elseif Y(ii-1)~=0
line1(k)=koY(ii-1);
end
k=k+1;
end
end
line1=line1(line1~=0);
word=[];
kd=0;
for k=1:2:length(line1)
Ik=imcrop(Ib,[1 line1(k) kol line1(k+1)-line1(k)]);
% figure, imshow(Ik);
hold on;
% vertikal projeksi
Ig=Ik;
bw=bwconncomp(Ik,4);
bw=labelmatrix(bw);
stats=regionprops(bw,{'centroid','boundingbox'});
for kk=1:length(stats);
cent=stats(kk).Centroid;
plot(cent(1),cent(2),'bo','MarkerSize',5,'MarkerFaceColor','g');
boks=stats(kk).BoundingBox;
rect=[boks(1),boks(2),boks(3),boks(4)];
In=imcrop(Ig,rect);
In=imresize(In,[50 70],'cubic');
% In=bwmorph(In,'remove');
In=~In;
%imwrite(In,sprintf('hasil%d.tif',kd),'tif');
[x,y]=size(In);
huruf=read_char(In,y);
word=[word huruf];
kd=kd+1;
end
fprintf(fid,'%s\n',word);
% word=[];
end
that's the full listing code of function.m
the result of that is a textfile (.txt) that consists of characters from [word] variable.
but actually i want to write that characters into gui (to statictext or edittext component)
looking forward to your response. Thanks.
This is completely aside from your question, but if your function is truly called function.m, I strongly suggest you to rename it. I'm surprise that matlab actually lets you use that name since it is a reserved keyword.
A good name for a function is something that actually describes what it does.
Guillaume, Thanks for the advice. actually the name is not function but i just want to describe that the mfile function is the mfile where the operation will execute. I changed it before to another name (identify.m) but it still doesn't work like i want.

Sign in to comment.

 Accepted Answer

Ria - you really should post code that actually works. Your return value from this function is named a but nowhere in your function body do you actually initialize a. Should this be word instead? And if word is in fact the string text that you wish to write to your edit text box, then that is easy to do.
Suppose your function signature is
function [word] = identify(f)
and that this is saved to a file named identify.m. Now suppose you call this function from your GUI (let us say in a push button callback) as
function pushbutton1_Callback(hObject, eventdata, handles)
% initialize f
f = ...;
word = identify(f);
% update the edit text box
set(handles.edit1, 'String', word);
Is that similar to the code that you are using?

1 Comment

Hayes - Thank you so much! That's really help. Now the string successfully set to edittext.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!