Change "Items" in a Discrete Knob with another knob

조회 수: 4 (최근 30일)
Douglas Anderson
Douglas Anderson 2019년 9월 11일
댓글: Adam Danz 2020년 4월 23일
Hi,
I have a test App in App Designer, with 2 Discrete knobs, Knob and Knob2. I want to change the settings ("Items") on "Knob" by moving "Knob2". This was just set up in App Designer, and the following Callback for "Knob2" added with the switch. Why doesn't this work?
methods (Access = private)
% Value changed function: Knob2
function Knob2ValueChanged(app, event)
value = app.Knob2.Value;
switch value
case '1-10'
set(app.Knob,'Items',{1,2,3,4,5,6,7,8,9,10},'Value',{5});
case '11-20'
set(app.Knob,'Items',{11,12,13,14,15,16,17,18,19,20},'value',{15});
case '21-30'
set(app.Knob,'Items',{21,22,23,24,25,26,27,28,29,30},'value',{25});
end
end
enda
Thanks!
Doug Anderson
  댓글 수: 1
Adam
Adam 2019년 9월 11일
What is 'value' if you put a breakpoint in? What does happen given you say it doesn't work?
I've never worked with knobs in app designer, but it would surprise me if the 'value' field evaluated to '1-10' as a char.

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

채택된 답변

Adam Danz
Adam Danz 2019년 9월 11일
편집: Adam Danz 2019년 9월 11일
For Knobs (continuous)
There are a few things wrong with your callback function. I've listed them in order of importance.
  1. The "Value" of your switch-case is numeric but your cases are character vectors so none of the cases will ever be chosen. Use a series of if-elseif conditionals. You could also use a switch-case with logical comparisons as in this answer.
  2. "Items" is not a property of a knob. Instead, use the "MajorTicks" property which expects a vector (not a cell array).
  3. In addition to MajorTicks, set the "Limits" propery to maximize the full range of your knob
  4. The "value" property expects a scalar, not a cell.
Here's how the callback should appear (showing 1 line, you can adapt the rests)>
if value >=1 && value <= 10
set(app.Knob,'MajorTicks',[1,2,3,4,5,6,7,8,9,10],'Limits',[1,10],'Value',5);
elseif
% set()
else
% set()
end
For "Discrete Knobs"
  1. The 'Items" property is expected to be a cell array of strings or chars. You can use num2str with strsplit to convert your numeric vector to a cell array of chars.
  2. The "value" property is expected to be a char array that belongs to the updated Items list.
Here's how the callback should appear (showing 1 line, again)
switch value
case '1-10'
set(app.Knob3,'Items',strsplit(num2str([1,2,3,4,5,6,7,8,9,10])),'Value','5');
  댓글 수: 5
Douglas Anderson
Douglas Anderson 2020년 4월 23일
Adam, I am having a different problem now, may be just stupid!
I am making a new discrete knob in Design View, and trying to change the Items by clicking on the ... on the right. I change the "Selected", and it works fine, but if I change the text in the table, it changes what is in the Items box, but has no effect on what is on the screen. I click elswhere -- nada! Back to Off Low Medium High. Matlab 2019b now, haven't installed 2020a.
Thanks!
Adam Danz
Adam Danz 2020년 4월 23일
I suggest making a new question. You can copy the URL to the new question here so I know where to look when I get the time to look at it (tomorrow, likely).

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by