How to merge the waitbar into UIcontrol GUI

3 views (last 30 days)
I have a UIcontrol program like below
function [] = MyGui()
S.fh = figure('units','pixels',...
'position',[200 200 800 800],...
'menubar','none',...
'name','GUI_2',...
'numbertitle','off',...
'resize','off');
S.ls = uicontrol('Style','text',...
'String','INTERACTIVE PARAMETER UPDATE',...
'FontWeight','bold',...
'FontSize', 12,...
'position',[200 700,400,50]);
S.pb1 = uicontrol('style','push',...
'units','pix',...
'position',[350 400 100 40],...
'fontsize',12,...
'string','Start',...
'callback',{@start_call});
S.ed = uicontrol('style','edit',...
'unit','pix',...
'position',[220 300 100 40],...
'fontsize',12,...
'string','New String');
S.pb2 = uicontrol('style','push',...
'units','pix',...
'position',[500 300 100 40],...
'fontsize',12,...
'string','Add String',...
'callback',{@update_call,S});
%Function for getting the nTs value and passing into encdec
function [] = update_call(varargin)
% Callback for pushbutton, reads new string from edit box.
S = varargin{3};
addstr = str2double(get(S.ed,'string')); % The string to add to the stack.
if ~strcmp(addstr,'NaN')
if addstr > 0 && addstr < 100
addstr
%pass the value to encdec
else
msgbox('Please enter a number between 1 and 100','WARNING')
end
end
%Function for starting the main program
function [] = start_call(varargin)
MainProgram
I am doing following tasks 1. I press the start and "MainProgram" runs. (Its a long running process) 2. I put some value in edit text box and press the update 3. The value will be passed onto the "MainProgram" for update.
Now I have two questions 1. there is a waitbar in MainProgram and how to merge that in main GUI (Instead of having a seperate pop-up). (start_call) 2. If user enters "addstr" value and press update, how can I interrupt MainProgram to take this addstr value for further processing ?
Thanks. 2.
  4 Comments
Statisticalbeginner
Statisticalbeginner on 24 Feb 2016
@Walter Roberson ... sorry for the late reply. This is my Matlab version MATLAB Version 7.5.0.342 (R2007b). Please let me know if you know the solution for this.
Statisticalbeginner
Statisticalbeginner on 24 Feb 2016
Edited: Statisticalbeginner on 24 Feb 2016
@Geoff hayes...I am passing addstr to MainProgram using assignin and evlin...MainProgram has a while loop and inside the while loop I am updating the values as the user keeps on changing it. That is not the problem now. Now I have a waitbar inside my while loop(which is situated inside Mainprogram) and I am trying to mimic this waitbar functionality with a slider. I have deveoped a dummy program to check,
function varargout = myfig(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @myfig_OpeningFcn, ...
'gui_OutputFcn', @myfig_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before myfig is made visible.
function myfig_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to myfig (see VARARGIN)
% Choose default command line output for myfig
handles.output = hObject;
handles.min = get(handles.ed,'value');
handles.max = 10000;
handles.i = 0;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes myfig wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = myfig_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pb.
function pb_Callback(hObject, eventdata, handles)
% hObject handle to pb (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
value = str2double(get(handles.ed,'string'));
value
assignin('base','value',value)
% --- Executes on slider movement.
function sl_Callback(hObject, eventdata, handles)
% hObject handle to sl (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
set(handles.sl,'Max',handles.max);
set(handles.sl,'min',handles.min);
set(handles.sl,'value',handles.i);
% --- Executes during object creation, after setting all properties.
function sl_CreateFcn(hObject, eventdata, handles)
% hObject handle to sl (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function ed_Callback(hObject, eventdata, handles)
% hObject handle to ed (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of ed as text
% str2double(get(hObject,'String')) returns contents of ed as a double
% --- Executes during object creation, after setting all properties.
function ed_CreateFcn(hObject, eventdata, handles)
% hObject handle to ed (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pbs.
function pbs_Callback(hObject, eventdata, handles)
% hObject handle to pbs (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
myfile;
function [] = myfile(handles)
%MYFILE Summary of this function goes here
% Detailed explanation goes here
% prompt('Please enter a valid number to start');
h = waitbar(0,'Processing...');
% i = input('Please enter a valid number to start');
i = evalin('base','value');
while(i < 10000)
clc
i = i + 1;
waitbar(i/10000,h)
% set(handles.sl,'value',i/10000);
% guidata(hObject,handles);
end
close(h);
Pleas consider myfile as MyProgram, just for this example. and please consider this myfile function is not inside the GUI m file, but is it in the seperate .m file.(Integrating both into a single file is not at all possible as the myfile or MyProgram is a complex entity originally)

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 29 Feb 2016
Statisticalbeginner - rather than using a slider to record your progress, consider using an axes object which you would then draw an image representing the progress completed. For example, your GUI may have an axes for plotting something, a push button to kick-off the processing, and another axes to record progress. If you give your GUI a unique Tag (see the properties inspector for your GUI) and set its HandleVisibility property to on, then your main program can look for this GUI and update the axes that has been designated as the wait (or progress) bar.
function updateWaitBar
% find the GUI whose tag is GuiWaitBarExample (GUI HandleVisibility
% property must be set to on)
hGui = findobj('Tag','GuiWaitBarExample');
if ~isempty(hGui)
% get the GUI handles structure
handles = guidata(hGui);
% axes2 is the waitbar
if ~isempty(handles) && isfield(handles,'axes2')
% if the waitBarImg doesn't exist, then create it and the
% image graphics object
if ~isfield(handles,'waitBarImg')
handles.waitBarImg = uint8(ones(1,maxIters,3)*255);
handles.hWaitBarImg = image(handles.waitBarImg,'Parent',handles.axes2);
end
% reset to a white background
if k == 1
handles.waitBarImg(:,:,:) = 255;
end
% set the progress complete portion of the wait bar
handles.waitBarImg(:,k,1) = 255; % red
handles.waitBarImg(:,k,2) = 0; % green
handles.waitBarImg(:,k,3) = 0; % blue
% update the "drawn" image graphic object with the new data
set(handles.hWaitBarImg,'CData',handles.waitBarImg);
% ensure that the axes and ticks are hidden
bgClr = get(handles.GuiWaitBarExample, 'Color');
set(handles.axes2,'XTick',[],'YTick',[],'XTickLabe',[],'YTickLabel',[],'XColor',bgClr,'YColor',bgClr);
% save the updated handles structure
guidata(hGui,handles);
end
end
end
end
Note that the above function is nested within the parent main program GuiWaitBarExampleProg and so has access to the k and maxIters defined there. Run the attached GUI, press the button, and see what happens!
  1 Comment
Geoff Hayes
Geoff Hayes on 1 Mar 2016
I'm not sure what you mean by variable. You can set maxIters to whatever you want when this main program is called. I just set it to 100 as an example. If it needs to be something else it can be, but you will have to set it to that new (variable) value based on other inputs like your B.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!