MATLAB Answers

Unable to use the list box items as a UIAxes legend values in App Designer

6 ビュー (過去 30 日間)
AdamG2013468
AdamG2013468 2019 年 7 月 25 日
コメント済み: Adam Danz 2019 年 7 月 29 日
I have built an application that plots data from items selected from a listbox. The listbox has multi-select enabled so I can plot multiple items.
When I use the below code, the default legend item names are "data1", "data2" ,etc., but i want to be able to use the app.ActiveChannelsListBox.Items as the legend entries. I know i am able to change the legend names using the property app.UIAxes.Legend.String, but the problem is that if i have only 3 list box items selected i have no way of accessing only the selected variable names to assign as legend items.
legend(app.UIAxes)
if length(app.ActiveChannelsListBox.Value) == 1
plot(app.UIAxes, app.TimeData, app.ActiveChannelsListBox.Value{1})
elseif length(app.ActiveChannelsListBox.Value) == 2
plot(app.UIAxes, app.TimeData, app.ActiveChannelsListBox.Value{1})
hold on
plot(app.UIAxes, app.TimeData, app.ActiveChannelsListBox.Value{2})
hold off
%elseif statements repeated to allow plotting using up to 16 list box selections
end

  0 件のコメント

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

採用された回答

Adam Danz
Adam Danz 2019 年 7 月 25 日
編集済み: Adam Danz 2019 年 7 月 25 日
Use the "DisplayName" property in your call to plot(). Also, move the call to legend() after your're done plotting.
But first, app.TimeData seems to be your X values. What are your Y values? The app.ActiveChannelsListBox values are all strings which I suppose you want as legend strings.
plot(app.UIAxes, app.TimeData, y, 'DisplayName',app.ActiveChannelsListBox.Value)
legend(app.UIAxes)

  5 件のコメント

表示 2 件の古いコメント
Adam Danz
Adam Danz 2019 年 7 月 25 日
If you're allowing for multiple selections and you want the Nth selected item to be the label for the Nth line object,
labs = app.ListBox.Value;
plot(...,'DisplayName',labs{1})
AdamG2013468
AdamG2013468 2019 年 7 月 29 日
app.ActiveChannelsListBox is the handle to my listbox. See the below for all the handle properties.
ProcessDTS_Snip2.PNG
The above properties are for when three items are selected from the listbox, in this case they are channels 0, 1, and 2.
ProcessDTS_Snip3.PNG
Adam Danz
Adam Danz 2019 年 7 月 29 日
I see what's happening. I didn't know you were using the ItemsData property which is usually empty. That screenshot of the property values is what I needed.
app.ListBox.Value usually contains the string(s) you just selected from the listbox. However, when you're using the ItemsData property, it contains those data instead.
Unfortunately there isn't a property that tells you which rows were selected. Usually you can match the string in app.ListBox.Value with the strings in app.ListBox.Items to find the row number. But that's more complicated when you have to match the ItemsData.
It's a really dumb issue with the list boxs.

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by