How To Migrate / Connect an Uicontrol Spinner Function To GUI created by GUIDE

조회 수: 4(최근 30일)
Dear Community,
I have a question about how to connect Uicontrol Function of Spinner to be displayed in my GUI created by GUIDE. I just so confused because GUIDE didnt provide a spinner and it provided by App Designer Matlab. I just want to create a spinner by a function and display it on my GUIDE.
And this is an example of spinner function i get from internet :
function [obj] = uispinner(varargin)
%UISPINNER GUI Control to accept a scalar for the user
% The spinner has up/down buttons which the user can press to
% adjust the value with the mouse. The callback is continuously
% called as he does this.
% 'Min' - minimum value (default 0)
% 'Max' - maximum value (default 1)
% 'Step' - increment (default 0.01)
% 'Position' - position matrix
% 'Value' - the initial value (default 0)
% 'Format' - printf format controls display of value (default '%g')
% 'Callback' - callback of form (hObject, eventdata, value ...)
% (callback is called continuous as users spins the spinner.
% Extra arguments can be passed in a cell array.)
% Example use uispinner('Max' 100, 'Step', 1, 'Format' '%03d', ...
% 'Callback', {@spinme, 'Fred', 'Bloggs'})
% function spinme(hOBject, eventdata, value, opt)
% fprintf(1, '%s %s is spun to %f\n', opt{1}, opt{2}, value);
% end
val = 0;
min = 0;
max = 1;
step = 0.01;
pos = [80 80 100 25];
callback = [];
fmt = '%g';
for ii =1:2:nargin
if(ii == nargin)
error('option without value (%s)', varargin{ii});
if(strcmpi(varargin{ii}, 'Value'))
val = varargin{ii+1};
elseif(strcmpi(varargin{ii}, 'Min'))
min = varargin{ii+1};
elseif(strcmpi(varargin{ii}, 'Max'))
max = varargin{ii+1};
elseif(strcmpi(varargin{ii}, 'Step'))
step = varargin{ii+1};
elseif(strcmpi(varargin{ii}, 'Position'))
pos = varargin{ii+1};
elseif(strcmpi(varargin{ii}, 'Callback'))
callback = varargin{ii+1};
elseif(strcmpi(varargin{ii}, 'Format'))
fmt = varargin{ii+1};
error('Urecognised option %s', varargin{ii});
if(val < min)
val = min;
if(val > max)
val = max;
obj = uipanel('Position', pos);
hedit = uicontrol('Style', 'edit', 'String', sprintf(fmt, val), ...
'Position', [pos(1), pos(2), pos(3)-10, pos(4)], 'Callback', @edited, ...
'BackgroundColor', 'white');
if(step ~= 0 && min < max)
sstep = [step/(max-min) 0.1];
sstep = [0.001 0.1];
if(max > min)
sval = (val-min)/(max-min);
sval = 0;
hslider = uicontrol('Style', 'slider', ...
'Position', [pos(1)+pos(3)-10, pos(2), 10, pos(4)], ...
'Callback', @slide, 'SliderStep', sstep, 'Value', sval);
cbdata = callback;
callback = cbdata{1};
cbdata(1) = [];
cbdata = [];
s = struct('hedit', hedit, 'hslider', hslider, 'min', min, 'max', max, ...
'val', val, 'step', step, 'callback', callback, ...
'cbdata', {cbdata}, 'fmt', fmt);
set(obj, 'UserData', s);
set(hslider, 'UserData', obj);
set(hedit, 'UserData', obj);
function slide(hObject, eventdata)
t = get(hObject, 'Value');
hpanel = get(hObject, 'UserData');
s = get(hpanel, 'UserData');
v = s.min + (s.max-s.min)*t;
if(s.step ~= 0)
v = v - mod(v, s.step);
if(s.step == fix(s.step))
v = floor(v + 0.5);
set(s.hedit, 'String', sprintf(s.fmt, v));
s.val = v;
set(hpanel, 'UserData', s);
s.callback(hpanel, [], s.val);
s.callback(hpanel, [], s.val, s.cbdata);
function edited(hObject, eventdata)
t = get(hObject, 'String');
hpanel = get(hObject, 'UserData');
s = get(hpanel, 'Userdata');
v = str2double(t);
if(isnan(v) || v < s.min || v > s.max)
set(s.hedit, 'String', sprintf(s.fmt, s.val));
if(s.step ~= 0)
v = v - mod(v, s.step);
s.val = v;
set(s.hedit, 'String', sprintf(s.fmt, s.val));
set(hpanel, 'UserData', s);
set(s.hslider, 'Value', (v - s.min) /(s.max-s.min));
s.callback(hpanel, [], s.val);
s.callback(hpanel, [], s.val, s.cbdata);
If the code above is running, then the result would be like this :
The spinner would appear in Figure 1 Uipanel. In my question, i want to migrate and put the spinner in Figure 1 to FMI_methods.fig (the first picture) as one of handles structure in it.... Would anyone help me out to find the code or the solution? Iam very grateful if anyone can solve my problem.... Thank you so much.... /.\ /.\ /.\

채택된 답변

Rik 2022년 1월 27일
What you want is not possible with the builtin uispinner function. As its documentation states the parent can only be a uifigure or one of its decendants. It is thus not possible to implement it in a GUIDE-created GUI.
If you have an implementation from someone else, you should ask them for usage examples. It is also a good idea to avoid the function names Matlab has chosen, so you should probably rename the function if you actually intend to use it.
Also, you shouldn't be using GUIDE. For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
  댓글 수: 3
Tyann Hardyn
Tyann Hardyn 2022년 2월 1일
@Rik Thank you so much, iam following your HINTS, and it work. Logic , Debug, and Behavior is The Key of all programming language, thought....

댓글을 달려면 로그인하십시오.

추가 답변(0개)


Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by