フィルターのクリア

Is it possible to plot the 'i'th set of data from a switch case with a single block of code?

2 ビュー (過去 30 日間)
I'm trying to use a drop down menu to display data from a particular specimen. The cases defined in the app designer UI component are "1", "2", "3", etc. I'm not sure I'm going about this the right way, but I'd like to be able to show data from the 'i'th specimen without making a case block for each number.
switch app.Specimen.Value
case app.Specimen.Value
i = app.Specimen.Value
cla(app.UIAxes);
plot(app.UIAxes, app.data.spec(i).si(:,10), app.data.spec(i).si(:,14));
end

採用された回答

dpb
dpb 2022 年 8 月 24 日
If it's the same code identically for each case but with a different dataset based on the index, that's all you need -- you don't need a switch construct at all -- just use the index. You don't even need the temporary "i" index variable, but it may be handy just to shorten the typing...
...
i = app.Specimen.Value
cla(app.UIAxes);
plot(app.UIAxes, app.data.spec(i).si(:,10), app.data.spec(i).si(:,14));
As long as the two fixed column numbers are fixed, that should be all you need to do...though I'd recommend to also make those variables with some more meaningful identification names instead of burying magic constants in the code itself. Besides the local documentation, makes fixing them if there's a need to change the data storage simpler if they're always referred to and defined in one place. They could be likely candidates to be globals app variables defined in startup code if needed more than one place.
  1 件のコメント
RGB85
RGB85 2022 年 8 月 24 日
Seems I was overcomplicating things. Your solution works. Thanks for your help!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by