MATLAB: App designer: Unbalanced or unexpected parenthesis or bracket

appappdesignerdesigner

Hi everyone,
I'm trying to learn how to use the AppDesigner.
In my app, i'm using the uigetfile command in a public property to obtain the filenames and path of multiple files. But it always gives me the "Unbalanced or unexpected parenthesis or bracket", even when it doesn't appear when I use the exact same command in the command window. This is it:
[filename,pathname]=uigetfile('*.mat','MAT-files (*.mat)', 'MultiSelect', 'on');
If I only put filename=uigetfile… it works. But I need the filename and the pathname.
Anyone knows why this happens?
Thank you!

Best Answer

This is happening because of the "[" that is at the beginning of the line. In MATLAB classes (how App Designer applications are represented) you cannot define properties in this way. This page explains more about how MATLAB classes use properties. If you want the filename and pathname to be properties of your application, then you can declare them in the Properties block and then define them later on with the uigetfile.
For example:
properties (Access = private)
filename
pathname
end
Then, in add the "StartupFcn" callback to your figure. You can do this by right-clicking the figure in the Design View and selecting Callbacks > add StartupFcn Callback or in the Code View by pressing in the "Callbacks" tab of the Code Browser.
In the StartupFcn, you can add the code for the uigetfile and save the file name and path to the app's properties.
function startupFcn(app)
% app.UIFigure.Visible = 'off';
[app.filename,app.pathname]=uigetfile('*.mat','MAT-files (*.mat)', 'MultiSelect', 'on');
% app.UIFigure.Visible = 'on';
end
The two commented out lines change the visibility of the figure, so you can uncomment them if you want the file selector to open before the rest of your application and then, once the user selects the file, the app will show up. If they are commented out, then both windows will open and you cannot guarantee that the user of your app selects or cancels from the file selector before the rest of the app is used.