uigetfile in appdesigner freezes MATLAB

In a button click function , I have the following lines of code
file=uigetfile;
app.fileToImport =file;
drawnow;pause(0.05);
The uigetfile somehow pauses the shell. I saw the following error:
Operation terminated by user during matlab.ui.internal.dialog.FileChooser/blockMATLAB (line 390)
In matlab.ui.internal.dialog.FileChooser/showPeerAndBlockMATLAB (line 115)
blockMATLAB(obj);
In matlab.ui.internal.dialog.FileChooser/show (line 122)
obj.showPeerAndBlockMATLAB();
In uigetputfile_helper (line 56)
ufd.show();
In uigetfile (line 130)
[filename, pathname, filterindex] = uigetputfile_helper(0, varargin{:});
In gui2/ImportDataButtonPushed (line 279)
file=uigetfile;
In appdesigner.internal.service.AppManagementService/tryCallback (line 362)
callback(app, event);
In matlab.apps.AppBase>@(source,event)tryCallback(appdesigner.internal.service.AppManagementService.instance(),app,callback,requiresEventData,event)
(line 37)
newCallback = @(source, event)tryCallback(appdesigner.internal.service.AppManagementService.instance(), ...
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 386)
Interrupt while evaluating Button PrivateButtonPushedFcn.
I added the drawnow and pause because I saw that solution somewhere but it does not seem to work. uigetfile worked everytime when I used it out of appdesigner but now it somehow causes the error everytime. I can resume the program somehow and it still works but I do not understand why it is freezing the operation.
I also found the following line
com.mathworks.mwswing.MJFileChooserPerPlatform.setUseSwingDialog(1)
But matlab does not suggest using it. What could be the reason for this and how can it be resolved?

7 Comments

I've not used R2021x, still with R2020b/Win10, but I've seen no such issues with the following callback function content...
...
filestr=fullfile(app.distrPath,'*Distr*.xlsx');
[app.distrFile,app.distrPath]=uigetfile(filestr,'Select Desired Distribution File',"MultiSelect","off");
app.distrdQualFile=fullfile(app.distrPath,app.distrFile);
...
There's other error checking code and saving data to the global restore data structure in the function, but no need of any machinations for updating graphics or anything in the routines; uigetfile works just as expected.
Other than I'd recommend being more explicit with fully-qualified file names, perhaps, don't see anytihng amiss.
Which OS and have you got automatic upadetes on, etc., ... I think this stuff interacts w/ the Java runtime so perhaps there are issues there.
I made a mistake in the question. I am with R2020b/win10 as well.
I tried the code multiple times and saw the following:
[file,path] = uigetfile ('*.txt');
This works everytime and has no problem.
app.fileToImport=file;
When I add the line above, it crashes (not everytime but randomly with the same error I explained above). Further this also minimizes the app. I solved the minimize issue with a figure line. I am not sure about the random errors yet. I am thinking it is just the license server that I am losing connection with. I want to understand it though.
I guess I'd ask to see the complete callback function -- and where app.fileToImport is created.
If I understand, you're saying the uigetfile dialog completes normally as long as you don't try to write to the app struct variable, but when you try to address it, then the app hangs?
How about using another variable there (ignoring the functionality for a moment; just to see where the issue might be) instead -- does execution then continue normally excepting you don't have the gobal variable set?
Here's what I am trying to do:
properties (Access = private)
data
fileToImport
end
function data = getdata(app)
temp=importdata(app.fileToImport);
end
function ImportDataButtonPushed(app, event)
[file,path] = uigetfile ('*.txt');
app.fileToImport=file;
app.data=app.getdata();
end
And yes, I tried it with a local variable and tried to pass that in the function getdata to see if that solves the issue. It is still the same. It does not help. What is really interesting is how the freezing does not happen everytime. Once the shel freezes, if I stop it in the terminal and continue again, it works just fine.
Your getdata function doesn't have a return value to assign to app.data -- it loads to a local variable and then just exits so temp is then out of scope. app.data is never set, and is called w/o an argument which is also expected.
I'd do more like --
properties (Access = private)
data
fileToImport
end
function getdata(app)
app.data=importdata(app.fileToImport);
end
function ImportDataButtonPushed(app, event)
[file,path] = uigetfile ('*.txt');
app.fileToImport=fullfile(path,file);
app.data=app.getdata(app);
end
with your organization although I've always just passed the file name to an external routine and let it read the data and operate on it instead of creating the global for the working data. Shouldn't make any difference there, but you do need to load your global in synch with the button; I've a feeling that's tied into the symptoms you're seeing; I'm surprised you don't get other errors about empty content initially.
The function getdata does return data for app.data after some manipulation on temp. I have just not included the line here.
So the function is something like
function data = getdata(app)
temp=importdata(app.fileToImport);
data = temp;
end
I think this is inline with what you wrote above.
"but you do need to load your global in synch with the button;" Sorry, I do not get what you mean by that.
dpb
dpb on 15 Jun 2022
Edited: dpb on 16 Jun 2022
In the code you posted the app.data variable was never being set so you weren't in synch with having data with the button callback...now it is it's there, but I'd not use importdata for this because it can return different formats of the data depending upon the file without notification -- and I'd also strongly recommend to use a fully-qualified file name. I don't know that either of those would trigger the symptoms you're seeing, but they're weak points in the existing code.
We can only see what you post; if that doesn't match the exact code in the app then it's easy (almost guaranteed?) to draw incorrect inferences.
I'd suggest building an independent app with just the uigetfile call and reading the file and then see if the symptom follows -- or if it is only tied into the specific app that indicates there's something elsewhere funky in it. If the symptom is repeated, that would imply something in the OS/Java/the MATLAB install itself.

Sign in to comment.

Answers (2)

uigetfile is supposed to open a file selection dialog box and let the user select a file? Do you not see that dialog box pop up? It is supposed to pause execution to wait for this input from the user.

1 Comment

Yes, I do see the dialog box. It is after i press select/open in the dialog box that MATLAB freezes.

Sign in to comment.

Co Melissant
Co Melissant on 17 Apr 2023
Edited: Co Melissant on 17 Apr 2023
just add:
com.mathworks.mwswing.MJFileChooserPerPlatform.setUseSwingDialog(1)
The infamous other solution, that also worked for me, and proposed by Yair https://undocumentedmatlab.com/articles/solving-a-matlab-hang-problem
drawnow;
pause(0.5);
drawnow;

Categories

Find more on App Building in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 13 Jun 2022

Edited:

on 17 Apr 2023

Community Treasure Hunt

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

Start Hunting!