App Designer で2つのドロップダウンリストを連動したい

8 Ansichten (letzte 30 Tage)
Haruhito Kato
Haruhito Kato am 6 Okt. 2022
Kommentiert: Haruhito Kato am 9 Okt. 2022
AppDesignerで試しにアプリを作っています。あるドロップダウンリストの結果に応じてもう一つのドロップダウンリストの中身を変えたいです。可能でしょうか?

Akzeptierte Antwort

Atsushi Ueno
Atsushi Ueno am 6 Okt. 2022
Bearbeitet: Atsushi Ueno am 6 Okt. 2022
可能です。具体的にはどのように連動させるのでしょうか?
2つのドロップダウンリストの項目数が同一で、同じ並びの項目になる様に連動する例を下記に示します。
下記の様に2つのドロップダウンリストのValueChangedコールバック関数を別々に定義し、「変更されていない側の項目」を、「変更された側の項目」と同じ並びになる様にインデックス番号で合わせてやれば良いのです。
properties (Access = private)
index % Description
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: DropDown2
function DropDownValueChanged1(app, event)
index = find(contains(event.Source.Items,event.Value)); % 変更された項目のインデックスを探す
app.DropDown.Value = app.DropDown.Items{index}; % 相手のドロップダウンリスト項目を合わせる
end
% Value changed function: DropDown
function DropDownValueChanged2(app, event)
index = find(contains(event.Source.Items,event.Value)); % 変更された項目のインデックスを探す
app.DropDown2.Value = app.DropDown2.Items{index}; % 相手のドロップダウンリスト項目を合わせる
end
end
または二者のコールバック関数を共通にし、どちらから呼ばれた場合でも両方の項目を設定すれば良いのです。
% Value changed function: DropDown, DropDown2
function DropDownValueChanged(app, event)
index = find(contains(event.Source.Items,event.Value)); % 変更された項目のインデックスを探す
app.DropDown.Value = app.DropDown.Items{index}; % 自分/相手のドロップダウンリスト項目を合わせる
app.DropDown2.Value = app.DropDown2.Items{index}; % 自分/相手のドロップダウンリスト項目を合わせる
end
  1 Kommentar
Haruhito Kato
Haruhito Kato am 9 Okt. 2022
無意識のうちに、app.DropDown.Valueは、直接書き換えられないから変更できないものかと思い込んでいました。
ありがとうございます!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Hiro Yoshino
Hiro Yoshino am 6 Okt. 2022
このあたりをみて、プロパティを変えるように設定すれば、上手く行きそうな気がしますが。試されましたか?

Kategorien

Mehr zu App Designer を使用したアプリ開発 finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!