Make the slider as a progress bar in matlab GUIDE

会有一股神秘感。 提交于 2019-12-12 04:27:01

问题


I am having hard time to make slider to behave as a progress bar. I have two push buttons. One accepts the starting value from a text editor(User Input) and other start the progress of the loop. Then I have got a slider, which I am trying to move as the function loop changes 'i' value(Update along/show progress).

This is my code.

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)
     handles.i = i;
     set(handles.sl,'value',i/10000);
     guidata(hObject,handles);
end
close(h);

Matlab verstion: 2014b

First: I want to learn how can I handle this when function is nested inside the GUI file.

Second: If you move the function into a different .m file.[Using set/get/guidata etc... and not passing input/output with function call]

Please let me know.


回答1:


For the first case, you will need to modify your function myfile. Before the while loop, you'll need to set the min and max of the slider. This will allow you to use i as your value.

set(handles.sl,'Min',1,'Max',10000);

To allow the slider to update, you will need to add refresh and drawnow inside your loop. Matlab will wait to draw ui changes until the end of your code if you so not use these. Your loop should look like the following:

while(i < 10000) clc i = i + 1; waitbar(i/10000,h) set(handles.sl,'value',i/10000); refresh; drawnow; end

From personal experience, the second case can get messy for larger applications. I would recommend creating your application using object oriented programming if you need to use more than one file. You could store ui handles in the objects you create, which keeps your code organized and makes it easier to pass handles back and forth.



来源:https://stackoverflow.com/questions/35421213/make-the-slider-as-a-progress-bar-in-matlab-guide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!