MATLAB: How to set up a uigetdir with a Push button in appdesigner

appapp designerappdesignercallbackfunctionsmatlab functionpropertyuigetdir

Hi All
I need to have a push button that when I press, the uigetdir command activates and allows the user to choose the desired directory , and pass this to the main code that will be run via the app. I have been unsuccessful and I get errors

Best Answer

Step 1: Add callback function to the Button
Right click the button from within appdesigner and add a callback function.
Step 2: add 'selectedPath' as a private property
This variable will store the user's selected path. You can name the variable anything you want.
From the Code View in appdesigner, go to the CODE BROWSER, select Properties, and press the green "+" button.
This will add a new private property. Rename it to selectedPath or whatever other name you want and set a default value. The default value will be used if this variable is accessed prior to the user choosing a path. The default value can be empty (selectedPath = '';). Add a commented description of the variable.
Step 3: Call uigetdir from the button's callback function
Call the uigetdir function from within the button's callback function. Choose which inputs you need. Store the output in app.selectPath using the same variable name as you declared as a private property.
function ButtonPushed(app, event)
app.selectedPath = uigetdir(); % <-- add this line
end
Step 4: Access the chosen path
From anywhere in your app, you can access the selected (or default) path by using
app.selectedPath