Force matlab gui to update ui control mid-function - matlab

I'm working on a gui using GUIDE in MATLAB, and from what I've read it looks like MATLAB updates the UI controls based on a timer every so often. Is there a way to force it to update the UI controls, so I can make it update in the middle of the function? Right now I have a function that does, simplifed, something like
set(handles.lblStatus,'String','Processing...')
%function that takes a long time
set(handles.lblStatus,'String','Done')
Since MATLAB doesn't update the GUI during a Callback function, the user only ever sees 'Done' after a long period of waiting and never sees 'Processing'. I tried adding guidata(hObject, handles) after the first set, hoping it would force the screen to update, but it doesn't.

Try calling DRAWNOW.
set(handles.lblStatus,'String','Processing...')
drawnow
%function that takes a long time
set(handles.lblStatus,'String','Done')

I believe there is a drawnow function in matlab.
drawnow completes pending drawing events

Related

MATLAB Slider GUI with Background While Loop to Control Feedback Servo Motors

I am attempting to implement a servo motor with feedback control program in MATLAB for multiple servo motors. The objective is to read servo(s) analog output, compare with a user value from slider bar(s), write the new position to the servo, and continue to write or monitor analog output to ensure servo maintains last user value. I am having trouble because I cannot figure out the optimal flow to always keep the GUI open and accepting user values while a while loop runs in the background. Below is the general structure that I would like the code to output:
test_gui %calls GUI figure with slider bar
GUI window with Slider bar pops up
%Callback for when user slides bar
function slider(i)_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
load('user.mat'); %Load previous user input
sze=size(user,1);
user(sze+1,1)=get(hObject,'Value'); %Add new user input to previous input
save('user.mat','user') %Save variable to be accessed by outer loop
'user' is passed into a continuous while loop that writes servo position
while user ~= 'c' %Continue to run until close window callback
load('user.mat'); %load user input from callback
Write_Servoi(a,user(end,:),add,speed,pinin,myServo) %Writes to servo(s) based on last input, allows servo(s) to maintain position if torqued out of position
end
During this while loop, I would like the user to be able to continuously change the slider and send this slider value into the while loop to be written to the servo. However, I cannot figure out how to update user when in the while loop.
If anyone has any ideas on how to get this to work, please let me know. I would greatly appreciate any help. I am open to changing the structure, as long as the objectives above are satisfied.
Robert
To update the user variable, you'll need to make sure it is properly loaded. Calling load with no output does not always load your variable into the function workspace.
Instead of load('user.mat');, you'll need to load your file as a structure and then retrieve your variable from the structure. To accomplish this, do the following:
s=load('user.mat');
user=s.user;

Push button is only changing position once

I have a question about callback functions in MATLAB's GUIDE. I have the following code execute when a button is pushed:
handles.xPos=handles.xPos+1
addX = handles.xPos
handles.shape2 =fill ([-2+addX 1+addX 1+addX -1+addX], [1 1 -1 -1], 'r');
This works, but only once (and the old shape is still there, but that is a separate problem). I have done debugging code and have determined that the callback function is always called when the button is pushed, but for some strange reason there is no effect in the change of the position after the first push of the button.
What am I doing wrong here?
You have to update your handles via guidata to take the modifications of handles into account:
guidata(hObject,handles);
Otherwise the modifications of handles are lost at the end of the callback's execution.
Best

Conditional pausing in Matlab (not Debugging)

I am new to Matlab so please bear with me.
So I created a two GUID GUI in which one generates a dynamic data and updates the plotting every second so I used pause(1) (which is continuous) and the second one takes the full data of the first GUI and plots it (opens on button press)
Is there a way where if I open the the second GUI the first GUI pauses and if and only if the second GUI is stopped the first GUI resumes its process?
Thanks in advance.
Update
Example:
gui1.m
function guie1()
for ii=1:100
c = magic(ii)
plot(c);
% a button at this point
% Some pause condition
drawnow;
end
so when I click on that button it would open a window (a new figure may be) so unless I close is the loop should be paused.
Here is the example:
run(fullfile(docroot,'techdoc','creating_guis','examples','callback_interrupt'));
Here is the link:
http://www.mathworks.com/help/matlab/creating_guis/callback-sequencing-and-interruption.html
Update:
Here:
http://blogs.mathworks.com/videos/2010/12/03/how-to-loop-until-a-button-is-pushed-in-matlab/
http://www.mathworks.com/matlabcentral/fileexchange/29618-spspj

Set uibutton properties for a MATLAB button

I am trying to set the background color of my gui button to a different color while the program is busy doing the computation. If I just set the color to change when the button is clicked it changes; However if I add another line of code AFTER the actual computation to change the color back to it default, the color never gets changed the first time.
function FitData_button_Callback(hObject, ~, handles) %#ok<DEFNU>
set(handles.FitData_button,'BackgroundColor',[0 204 0]./255,'String','Fitting Data');
% do some computation that takes time here...
% this line causes the first instance of 'set' not to work
set(handles.FitData_button,'BackgroundColor',[237 237 237]./255,'String','Fit Data');
If I change the properties for the second time I call 'set', it does change from the default to what was specified the second time (and it does this after the long computation is finished). However, the first set of properties never get assigned.
So it seems like I am only allowed to set the BackgroundColor and String properties one time, which does not make any sence.
What gives?
thanks
The short answer is to put a "drawnow" in the middle of your computations. The long answer is best illustrated by Yair Altman on his blog (which is FANTASTIC by the way) - http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/

Display images within a loop without user intervention

In Matlab, I have a loop that performs operations on arrays.
I would like to display the array at each iteration (using "imagesc" for instance), but without needing user intervention.
I can force Matlab to update the displayed figure by inserting the command "pause" after imagesc, but it needs to be dismissed by a key press.
Without the "pause" command, the figure is not updated before the end of the loop.
Is there a way to update the figure at each iteration of the loop ?
Try using Matlab command drawnow after the graphing code within the loop.
drawnow causes figure windows and their children to update, and
flushes the system event queue. Any callbacks generated by incoming
events (e.g., mouse or key events) are dispatched before drawnow
returns.
If drawnow updates too quickly, you can control the "frame rate" slightly better with pause(time_in_seconds). E.g., to pause for 0.5 seconds use
for ...
% plot stuff
pause(0.5);
end