uieditfield defined in classdef won't update Value

2 visualizaciones (últimos 30 días)
Alex
Alex el 14 de Feb. de 2024
Comentada: Umang Pandey el 16 de Feb. de 2024
I'm creating an app defined within a class. I click a uibutton and within it's callback select a directory. I want to update the Value field in my uieditfield to display the path to the directory, but the text isn't appearing in the edit field.
classdef MyClass
properties
mainfig
mapgrid
dirbutton
dirdisplay
end
methods
function obj = MyClass()
obj.mainfig = uifigure();
obj.mapgrid = uigridlayout(obj.mainfig, [1 1]);
obj.dirbutton = uibutton(obj.mapgrid);
obj.dirbutton.ButtonPushedFcn = @obj.dirbutton_callback;
obj.dirdisplay = uieditfield(obj.mapgrid);
obj.dirdisplay.Placeholder = "(Choose a directory)";
end
function dirbutton_callback(obj, src, event)
thisdir = uigetdir(pwd, 'Select a folder');
if thisdir == 0
disp('No directory selected. Exiting script...')
return
end
obj.dirdisplay.Value = thisdir;
end
end
end
How do I get the displayed text to update?

Respuestas (1)

Umang Pandey
Umang Pandey el 15 de Feb. de 2024
Hi Alex,
I understand that on clicking the button and selecting the directory, you intend to reflect the path to selected directory in the "uieditfield."
The reason behind the code not updating the "uieditfield" is because "MyClass" is a value class. It has not inherited a handle class in the class declaration. In MATLAB, the "< handle" syntax is used to indicate that a class is a handle class. A handle class allows multiple variables to refer to the same object, and changes made to the object through one variable will be reflected in all other variables that reference the same object. This is in contrast to value classes, where each variable has its own independent copy of the object.
You can incorporate the following change in your code to get it working:
classdef MyClass < handle
properties
mainfig
mapgrid
dirbutton
dirdisplay
end
methods
function obj = MyClass()
obj.mainfig = uifigure();
obj.mapgrid = uigridlayout(obj.mainfig, [1 1]);
obj.dirbutton = uibutton(obj.mapgrid);
obj.dirbutton.ButtonPushedFcn = @obj.dirbutton_callback;
obj.dirdisplay = uieditfield(obj.mapgrid);
obj.dirdisplay.Placeholder = "(Choose a directory)";
end
function dirbutton_callback(obj, src, event)
thisdir = uigetdir(pwd, 'Select a folder');
if thisdir == 0
disp('No directory selected. Exiting script...')
return
end
obj.dirdisplay.Value = thisdir;
end
end
end
To learn more about Handle classes, you can refer to the following documentation:
Hope this helps!
Best,
Umang
  1 comentario
Umang Pandey
Umang Pandey el 16 de Feb. de 2024
Hi Alex, do consider accepting/upvoting the answer if it is was helpful.
Best,
Umang

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by