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

1 次查看(过去 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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by