フィルターのクリア

Is there a way to edit the own items of an editable uidropdown, where mutiple items can be the same value?

1 回表示 (過去 30 日間)
I made a quick example to demonstrate my problem.Is there a way to make something like this:
fig = uifigure;
dropdown = uidropdown(fig,"Editable","on");
dropdown.Items = ["Test_1","Test_2","Test_3","Test_3"];
dropdown.ValueChangedFcn = @(src,event)example_function(src,event);
function example_function(src,event)
% currently something like this is not implemented and in my understanding there isn't a workaround for it
% in this special usecase where multiple identically items can be in
% one instance of uidropdown(why a workaround likely isn't possibly)
% Is there a reason why something like this isnt implemented:
% dropdown.Items{selected/clicked on} -> event.IndexOfClickedItem: ["Test_1"->1, "Test_2"->2, "Test_3"->3, "Test_3"->4]
index = event.IndexOfClickedItem;
src.Items{index} = src.Value;
end
  2 件のコメント
Mario Malic
Mario Malic 2023 年 5 月 30 日
Check ItemsData property, it may help you to index into Items.
Jonas Schütz
Jonas Schütz 2023 年 5 月 30 日
編集済み: Jonas Schütz 2023 年 5 月 30 日
I already tried it with ItemsData but there isn't a specific way to acess the needed index and at the end you get the same problem like described.I tried a workaround using ItemsData but at the end it doesn't work because there can be multiple same Items. Or is there something like src.ItemsData.Value for the ValueChangedFcn?

サインインしてコメントする。

採用された回答

Cris LaPierre
Cris LaPierre 2023 年 5 月 30 日
編集済み: Cris LaPierre 2023 年 5 月 30 日
There is a way to do this. As suggested, use the ItemsData property. You need to first define this when creating your drop down. Internally, the src and event values are the corresponding value from the ItemsData property.
fig = uifigure;
dropdown = uidropdown(fig,"Editable","on");
dropdown.Items = ["Test_1","Test_2","Test_3","Test_3"];
% Define the ItemsData property with a value for each element in Items.
dropdown.ItemsData = 1:4;
dropdown.ValueChangedFcn = @(src,event)example_function(src,event);
function example_function(src,event)
index = event.Value;
src.Items{index} = src.Value; % Be sure this statement is doing what you want
end
The final line of code in your function will generate an error. I believe you have to replace the entire array of Items, and not just a single value. To do what I think you are trying to do, you'll have to do that in steps. That might look like this:
Items = src.Items;
Items{index} = num2str(src.Value);
src.Items = Items;
  6 件のコメント
Cris LaPierre
Cris LaPierre 2023 年 5 月 30 日
Have you tested the code I shared? Seems to work for me, but perhaps I don't fully understand the issue you are trying to describe.
Jonas Schütz
Jonas Schütz 2023 年 5 月 31 日
編集済み: Jonas Schütz 2023 年 5 月 31 日
Sorry, it is indeed working as described. Thank you very very much! I wasn't at the PC and only had seen the
event.PreviousValue
with which I also had played around a bit. Thank you! I only had tested event.PreviousValue without ItemsData property but with the ItemsData property it behaves like expected.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by