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

2 ビュー (過去 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});
end
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};
else
error('Urecognised option %s', varargin{ii});
end
end
if(val < min)
val = min;
end
if(val > max)
val = max;
end
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];
else
sstep = [0.001 0.1];
end
if(max > min)
sval = (val-min)/(max-min);
else
sval = 0;
end
hslider = uicontrol('Style', 'slider', ...
'Position', [pos(1)+pos(3)-10, pos(2), 10, pos(4)], ...
'Callback', @slide, 'SliderStep', sstep, 'Value', sval);
if(iscell(callback))
cbdata = callback;
callback = cbdata{1};
cbdata(1) = [];
else
cbdata = [];
end
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);
end
if(s.step == fix(s.step))
v = floor(v + 0.5);
end
set(s.hedit, 'String', sprintf(s.fmt, v));
s.val = v;
set(hpanel, 'UserData', s);
if(isempty(s.callback))
return;
elseif(isempty(s.cbdata))
s.callback(hpanel, [], s.val);
else
s.callback(hpanel, [], s.val, s.cbdata);
end
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));
else
if(s.step ~= 0)
v = v - mod(v, s.step);
end
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));
if(isempty(s.callback))
return;
elseif(isempty(s.cbdata))
s.callback(hpanel, [], s.val);
else
s.callback(hpanel, [], s.val, s.cbdata);
end
end
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
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 件)

Community Treasure Hunt

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

Start Hunting!

Translated by