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

7 views (last 30 days)
Haruhito Kato
Haruhito Kato on 6 Oct 2022
Commented: Haruhito Kato on 9 Oct 2022
AppDesignerで試しにアプリを作っています。あるドロップダウンリストの結果に応じてもう一つのドロップダウンリストの中身を変えたいです。可能でしょうか?

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 6 Oct 2022
Edited: Atsushi Ueno on 6 Oct 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 Comment
Haruhito Kato
Haruhito Kato on 9 Oct 2022
無意識のうちに、app.DropDown.Valueは、直接書き換えられないから変更できないものかと思い込んでいました。
ありがとうございます!

Sign in to comment.

More Answers (1)

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

Categories

Find more on App Designer を使用したアプリ開発 in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!