How to deal with gui's and handles in .m

Basically from a gui i run with a function a .m file that contains a for cycle. Let's suppose that this cycle has 2 iterations. At the end of the first iteration i'd like to display in the gui the percentual of the routine, therefore have an output in the gui.
This is the part of the code that i have at the end of the for cycle in the .m file of the function
compl=(index/num_repetitions)*100;
assignin('base','compl',compl)
compl = evalin('base', 'compl');
handles=guidata(handles.percentual);
set(handles.percentual,'String',compl);
While this is the error i obtain
??? Undefined function or variable "handles".
Error in ==> sim_main_part2 at 138 handles=guidata(handles.percentual);
Error in ==> GUI>simulate_Callback at 237 [y]=sim_main_part2(ll, dz, stDdz, z, teta, Const_x, Const_y, PixScale, num_repetitions);
What should i do to make it work? Thank you for any help

Answers (2)

This line of code does not make much sense
handles=guidata(handles.percentual);
It is unclear what it should be out of context, but watch the video and you should be fine.

1 Comment

Michele
Michele on 11 Nov 2013
Edited: Michele on 11 Nov 2013
i am sorry but it did not help. it always says
??? Undefined function or variable "handles".
Error in ==> sim_main_part2 at 138 set(handles.percentual,'String',num2str(compl));
Error in ==> GUI>simulate_Callback at 237 [handles, y]=sim_main_part2(ll, dz, stDdz, z, teta, Const_x, Const_y, PixScale, num_repetitions);
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> GUI at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)GUI('simulate_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
and thank you for the help!

Sign in to comment.

You called sim_main_part2() but didn't pass in handles. Since it's not a GUIDE-generated callback, it does not automatically have handles in the private, local workspace of the sim_main_part2() function. If you want to use handles in that function, you will have to pass it in, and list it as an argument in the function declaration line:
function y = sim_main_part2(handles, ll, dz, stDdz, z, teta, Const_x, Const_y, PixScale, num_repetitions)
% Code goes here...
If you change handles, you can pass it out also:
function [handles, y] = sim_main_part2(handles, ll, dz, stDdz, z, teta, Const_x, Const_y, PixScale, num_repetitions)
% Code goes here...

15 Comments

i tried but it does not work in this way. Should i change something else in the code? thank you!
It does work but you must have done something wrong. If your code is in the comment to Doug, the wrong part is that when you called sim_main_part2(), you did not pass in handles. You need to declare it like I said then call it passing in handles (also like I said):
[handles, y] = sim_main_part2(handles, ll, dz, stDdz, z, teta, Const_x, Const_y, PixScale, num_repetitions);
The above is how you CALL it, not how you declare it which I already told you how to do in my first reply.
It does work but you must have done something wrong. If your code is in the comment to Doug, the wrong part is that when you called sim_main_part2(), you did not pass in handles. You need to declare it like I said then call it passing in handles (also like I said):
[handles, y] = sim_main_part2(handles, ll, dz, stDdz, z, teta, Const_x, Const_y, PixScale, num_repetitions);
The above is how you CALL it, not how you declare it which I already told you how to do in my first reply.
indeed i did not write it correctly! Anyhow now i don't have an error but i don't see the refresh of the data while the for cycle is still running. and even at the end of the cycle the value is still not uploaded.
Sometimes if number crunching is happening too rapidly it won't update guis. In that case put a drawnow call right after you want the gui to update/refresh/repaint and it will force it to be current.
that is the code i have not in the .m of the gui but in the .m of the function i call
function [handles, y]=sim_main_part2(handles, ll, dz, stDdz, z, teta, Const_x, Const_y, PixScale, num_repetitions);
...
for i=1:10
...
assignin('base','compl',compl)
compl = evalin('base', 'compl');
set(handles.percentual,'String',num2str(compl));
drawnow
end
buy the edit text (handles.percentual) is not updated and remains as setted at the beginning at 0. What should i do? Also write something in the callback of the edit text in the .m of the gui? Still thank you for following me in that problem. i do appreciate.
What is the point of
assignin('base','compl',compl)
compl = evalin('base', 'compl');
?? It is not needed at all.
I thought it was necessary, my fault. Thank you! Anyhow the problem remains.
What data are you trying to refresh? handles.percentual? Is it an edit text or a static text? Are you sure compl is changing each time? Does it update if you step through it a line at a time in the debugger?
Michele
Michele on 12 Nov 2013
Edited: Michele on 12 Nov 2013
It is an edit text (with 'tag'=percentual) and i want to write there value 'compl' that changes every cycle. it does change during the cycles. and it updates. the problem seems that it does not write the value in the text in the gui! I'd like to remark that in the .m file of the gui i call the function. In this function i have a for cycle and at the end of every iteration i would like to have this variable displaced in the edit text.
Then the code should work. I don't know what else to do unless you upload your files.
it does not unfortunately
Well I guess you're totally and completely out of luck, since it looks like you've stopped trying to solve it, or else you chose not to upload your files for some other reason (such as they're secret/proprietary).
actually i expected to solve it sooner, then other tasks rised. can you please give me an email address where to send the files? Then of course i will write here the solution to the problem.
You can attach files here. I don't take files via email. But I give no guarantee that I'll have time to look at them.

Sign in to comment.

Tags

Asked:

on 11 Nov 2013

Commented:

on 18 Nov 2013

Community Treasure Hunt

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

Start Hunting!