How do you put lcolorbar in UIAxes (app designer)?

I have a UIAxes area in an app using app designer. I want to plot some stuff colored by category using lcolorbar (colorbar with text labels) but it won't let me. The regular colorbar can be assigned to an axes by not lcolorbar. Is there a workaround?

 Risposta accettata

Let's say I want to place this in App Designer.
[Z,R] = readgeoraster("n39_w106_3arc_v2.dt1","OutputType","double");
[vis,visR] = viewshed(Z,R,39.6,-105.6,1000);
figure
usamap(vis,visR)
geoshow(vis,visR,"DisplayType","surface")
cmap = [0.4940 0.1840 0.5560; 0.4660 0.6740 0.1880];
colormap(cmap)
h = lcolorbar(["Obscured" "Visible"]);
h.Position(1) = h.Position(1)-0.1;
Here is a workaround:
[Z,R] = readgeoraster("n39_w106_3arc_v2.dt1","OutputType","double");
[vis,visR] = viewshed(Z,R,39.6,-105.6,1000);
fig = figure;
usamap(vis,visR)
geoshow(vis,visR,"DisplayType","surface")
cmap = [0.4940 0.1840 0.5560; 0.4660 0.6740 0.1880];
colormap(cmap)
h = lcolorbar(["Obscured" "Visible"]);
h.Position(1) = h.Position(1)-0.1;
for ii = length(fig.Children):-1:1
fig.Children(ii).Parent = app.Panel;
end
app.Panel.Children(1).Colormap = cmap;
app.Panel.Children(2).Colormap = cmap;
delete(fig)
I created the graphics on a figure (named fig) and then transfered each child of the figure to the a uipanel or uifigure. In this case, I chose app.Panel. I then had to add the correct colormaps. See app attached for working example.

5 Commenti

Thanks. If I were just setting this up once (i.e. it only did this at app startup), this may be a workable solution. However, the use-case I'm working with is that I have a list of different variables that have different types (numeric, logical, categorical) and the purpose of the app is so that I can select different ones to color the points in the plot as a way of exploring the dataset.
If I select a numeric variable then I'd want a regular colorbar, if I then select a categorical variable, then I'd want it to switch to using a lcolorbar. It would be very clunky if each time I switched, matlab had to quickly create a figure and then transfer over the objects to the app then close the figure.
I updated the app with a toggle button to alternate between the numeric and labeled colorbar. Let me know if a technique like this would work for you.
Another alternative route is to edit the Ticks and TickLabels of the regular colobar.
figure;
c = colorbar;
c.Ticks = 0:1/4:1;
c.TickLabels = categorical(["category 1","category 2","category 3","category 4","category 5"]);
I though of doing this, but the problem is for categorical there should be distinct colors in the colorbar instead of a color gradient. You can get colorbar to have distinct colors, but the ticks will appear between the colors which makes it ambiguous which label goes with which color. With lcolorbar the ticks are in the middle of the colors as they should be.
I tried the example_lcolorbar and this seems to do what I'm looking for. It's not the ideal solution, which would be matlab putting the axes argument in the lcolorbar so it works the same as colorbar, but it works.
If I clear/reset the uiaxes, which I have to do occasionally to set new plots, is there a way of keeping the lcolorbar object and re-associating it to a new axes and data set without reverting to spawning a new figure?

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2024a

Richiesto:

il 3 Lug 2024

Commentato:

il 4 Lug 2024

Community Treasure Hunt

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

Start Hunting!

Translated by