MATLAB: Matlab freeze when using a file dialog selection

file selectionfreezehangMATLABuigetfileuiputfile

I'm having a very frustrating problem that I can't seem to solve when using file selection dialogue.
Occaisionaly when I load a file selection dialogue from a button on a GUI (using 'uigetfile'), matlab freezes and can't be recovered. The only solution is to stop the process in windows closing matlab and anything that I was working on. I've also had the same issue when using 'uiputfile'.
The problem is not repeatable and appears to be random when it occurs. It doesn't seem to matter whether I've had matlab open for hours or shortly after opening.
The method that the button callback accesses is as follows, and as you can see I've tried to use some of the tricks already suggested on this community by inserting pause and drawnow. Unfortuantely neither of these has fixed the issue and it still randomly occurs.
%%Function to load existing data dictionary into the tool.
function LoadDD(obj, ~, ~)
% Go to the folder location of most recently accessed simulink
% model
try
NewPath=fileparts(get_param(bdroot, 'FileName'));
if ~strcmp('C:\Program Files\MATLAB\R2015b\toolbox\simulink\blocks\library', NewPath)...
&&~strcmp('C:\Program Files\MATLAB\R2015b\toolbox\stateflow\stateflow', NewPath)
obj.PathName = NewPath;
end
catch
end
drawnow;
pause(0.05); %This is to prevent matlab from hanging
[FileName,obj.PathName] = uigetfile('*.m','Select a model data dictionary', obj.PathName );
drawnow;
pause(0.05);
if FileName ==0
obj.PathName = 'C:\TEAM\PROJECTS\ControlSystemsSVN\Mathworks\';
return
end
% Do other stuff...
Has anyone had any experience with this issue, and found any proper solutions?
I'm using R2015b.

Best Answer

Try to use the following command before uigetfile.
com.mathworks.mwswing.MJFileChooserPerPlatform.setUseSwingDialog(1)
Let me know.
Related Question