【问题标题】:How To Use Matlab GUI Slider Trough如何使用 Matlab GUI 滑槽
【发布时间】:2017-01-02 06:35:04
【问题描述】:

我正在尝试浏览我加载到 GUI 中的图像。当图像加载到 GUI 中时,我像这样更新了滑块参数。

图片加载“功能”的一部分

    if handles.nImages > 1
        set(handles.frameSlider,'Min',1,'Max',handles.nImages,'Value',1)
        handles.sliderStep = [1 1]/(handles.nImages - 1);
        set(handles.frameSlider,'SliderStep',handles.sliderStep)
    end

然后尝试在图像中滑动,并且滑块箭头键工作正常,但是当我这样做时,拉动滑块槽不起作用。当我拉动滑块槽时,拉动很顺畅,没有任何步进增量的感觉。它给了我这个错误:Subscript indices must either be real positive integers or logicals。我认为发生这种情况是因为当我拉动槽时,我将其设置为values,介于允许的滑块增量之间,因为拉动不是逐步增加的。

滑块拉动“功能”的一部分

sliderPosition = get(handles.frameSlider,'Value');
imagesc(handles.imageListPhs{indexes})

可能是什么错误?

【问题讨论】:

  • 如果您不打算提供minimal reproducible example,您至少可以解释一下“不起作用”的含义。
  • @excaza 编辑了问题。
  • 错误信息有什么不清楚的地方?
  • @excaza 没有什么不清楚的。我不清楚的是如何在拉低谷时应用离散的步进增量。

标签: matlab user-interface slider matlab-guide


【解决方案1】:

滑块的步长仅控制用户单击箭头按钮或在滑块槽内时的行为方式。用户拖动时拇指的位置不受步长的控制,因此它很可能返回一个非整数,不能用作索引。您需要使用舍入函数,如 roundceilfloorfix 将滑块值转换为对索引有效的值。

考虑以下示例:

function testcode
nA = 15;

myfig = figure('MenuBar', 'none', 'ToolBar', 'none', 'NumberTitle', 'off');

lbl(1) = uicontrol('Parent', myfig, 'Style', 'text', ...
                'Units', 'Normalized', 'Position', [0.1 0.7 0.8 0.2], ...
                'FontSize', 24, 'String', 'Selected Value:');

lbl(2) = uicontrol('Parent', myfig, 'Style', 'text', ...
                'Units', 'Normalized', 'Position', [0.1 0.4 0.8 0.2], ...
                'FontSize', 24, 'String', 'Rounded Value:');

uicontrol('Parent', myfig, 'Style', 'Slider', ...
          'Units', 'Normalized', 'Position', [0.1 0.1 0.8 0.2], ...
          'Min', 1, 'Max', nA, 'SliderStep', [1 1]/(nA - 1), 'Value', 1, ...
          'Callback', {@clbk, lbl});
end

function clbk(hObject, ~, lbl)
slider_value = get(hObject, 'Value');
slider_value_rnd = round(slider_value);

set(lbl(1), 'String', sprintf('Selected Value: %.2f\n Can I Index with this? %s', ...
    slider_value, canIindexwiththis(slider_value)));
set(lbl(2), 'String', sprintf('Rounded  Value: %.2f\n Can I Index with this? %s', ...
    slider_value_rnd, canIindexwiththis(slider_value_rnd)));

set(hObject, 'Value', slider_value_rnd);  % Snap slider to correct position
end

function [yesno] = canIindexwiththis(val)

try
    A(val) = 0;
catch
    yesno = 'No!';
    return
end
yesno = 'Yes!';
end

这说明了这个过程:

【讨论】:

  • 感谢您非常详细的解释。
猜你喜欢
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 2015-03-21
  • 2016-02-11
  • 2015-10-03
  • 2016-03-08
  • 1970-01-01
相关资源
最近更新 更多