在Matlab GUIDE中将滑块设为进度条 [英] Make the slider as a progress bar in matlab GUIDE

查看:87
本文介绍了在Matlab GUIDE中将滑块设为进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难使滑块表现为进度条. 我有两个按钮.一个从文本编辑器(用户输入)接受起始值,另一个从循环进度开始. 然后我有一个滑块,随着功能循环更改'i'值(沿着/显示进度更新),我试图将其移动.

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).

这是我的代码.

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版本:2014b

Matlab verstion: 2014b

首先:我想学习当函数嵌套在GUI文件中时如何处理.

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

第二:如果将函数移到另一个.m文件中.[使用set/get/guidata等...并且不通过函数调用传递输入/输出]

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

请让我知道.

推荐答案

对于第一种情况,您将需要修改函数myfile.在while循环之前,您需要设置滑块的最小值和最大值.这将使您可以将i用作您的值.

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);

要更新滑块,您需要在循环内添加refreshdrawnow.如果您不使用这些更改,Matlab将等待绘制ui更改,直到代码结束.您的循环应如下所示:

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

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

根据个人经验,第二种情况对于大型应用程序可能会变得混乱.如果您需要使用多个文件,我建议使用面向对象的编程来创建您的应用程序.您可以将ui句柄存储在创建的对象中,这样可以使代码井井有条,并使来回传递句柄更加容易.

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.

这篇关于在Matlab GUIDE中将滑块设为进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆