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

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

dpb
dpb am 24 Aug. 2022
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.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by