uiputfile blocks all of MatLab any error it keeps repeating, it requires CTR-ALT-DEL end task (my app) to exit

1 visualización (últimos 30 días)
I wrote this code that ends up being an endless loop Due to weird treatment of this particular dialog
I can't add a breakpoint while (Save to) is open looking for a path from the call uiputfile(...
Code that I think should run fine is in an endless loop that can't be exited since all of MatLab all is locked out.
Even after CTR-ALT-DEL End task the dialog stays open, but at least with no calling app I can finally close it.
No Ctrl-C allowed anywhere, No response from Command Window, App Designer or my app itself, just endless beeps and reopening the dialog. Neither OK or cancel gets out of it but in the example below Cancel with throw an erro so you won't have my problem
Try this (modified code, removing app.propertied to work in your command window
The goal is to exit the while with a valid path (app.)OutLoc
startingFolder = "";
baseFileName = '*.csv';
OutLoc = ""; % Must use "" as isfolder thinks " " is a folder!
while ~isfolder(OutLoc) % Let's stay here until it's good
defaultFileName = fullfile(startingFolder,baseFileName);
% Show all files in the folder by using "*.*"
[baseFileName, startingFolder] = uiputfile('*.*', ...
'Save to', defaultFileName);
% Various ways of chacking throw wrong type errors so I settled on this
if size(baseFileName,2)<6 % Is baseFileName any good (or even a string)?
% User clicked the Cancel button. or didn't enter a file
uialert(app.MouseOdor,"No file name given","No Output File"); % Crash when not running it app
end
if ~isstring(startingFolder) % because uiputfile() will change it to 0 on cancel
startingFolder = "";
end
if isfolder(startingFolder)
OutLoc = startingFolder; % All good, should exit the loop now
MouseFolder = startingFolder; % actually saving it to app.MouseFolder.Value for later use
end
end % while
Error using uiputfile (line 102)
Support for Java user interfaces is required, which is not available on this platform.
I tried running it in here, so now I know it's a Java issue... lovely.
I guess you will have to copy and paste it to your command window and try yourself.
BTW, I'm in 2023b, probably time to upgrade
Also, when I ask a question like the above does Ask the Community page not allow any text into the Description* block.
How does it decide it doesn't like my question?
  6 comentarios
Swastik Sarkar
Swastik Sarkar el 23 de Sept. de 2024
Hi @Gavin,
Doesn't fill in startingFolder it's still empty if I give a file name with no extension.
I've tested this on MATLAB R2023b on both Windows and Debian 12 (Linux), but I haven't been able to reproduce the problem.
It would be helpful to have the code for the app to determine if the issue can be replicated.
dpb
dpb el 23 de Sept. de 2024
Editada: dpb el 23 de Sept. de 2024
input = " ";
input = fl::filesystem::absolute(input)
Invalid use of operator.
The example in the linked-to Answer doesn't work under R2021b.
I suppose one can make the argument they do that
isfolder(fn)
is evaluated as
isfolder(trim(fn))
but it's still imo unexpected behavior in MATLAB since a blank folder can't exist under Windows...
No way one will change TMW's mind on this one, however, so best one could do would be to ask for a note to be added to the documentation regarding that behavior. And, of course, it is true for all related functions, not just isfolder

Iniciar sesión para comentar.

Respuesta aceptada

Gavin
Gavin el 23 de Sept. de 2024
Editada: Gavin el 23 de Sept. de 2024
I changed from uiputfile() to uigetdir(startingFolder... as that is what I was really looking for: Where will the output files go. In App Designer uiputfile('*.*', ... Where the user input is a name with no extension it fails in MATLAB. But using uiputfile('*.csv', ... where, I guess Windoze fills in the extension, keeps MATLAB happy. So the issue is that uiputfile in App Designer MATLAB program can't handle a file with no extension.
Yes, wasn't the best code for what I needed but it should have worked and my alternative does, so I'm able to move on.
  1 comentario
dpb
dpb el 23 de Sept. de 2024
Editada: dpb el 23 de Sept. de 2024
" uiputfile in App Designer MATLAB program can't handle a file with no extension"
Note that App Designer has no bearing on the behavior of base MATLAB functions; they operate the same whether in or outside the graphical interface.
I don't see any failure in uiputfile with a returned name from the user with no extension --
>> [f,d,ix]=uiputfile('*.*')
f =
'foobar'
d =
'C:\Temp\'
ix =
1
>>
The returned filename is that typed in; the directory is the current working directory at the time the call is made if not given explicitly.
The problem in your code was mostly centered about the loop never terminating because the logic of changing the pertinent flag variable kept resetting it because of the test on the type of the returned variable always failing because uiputfile doesn't return a string but a char() vector.
If you change directories in the call, then, it returns the new changed-to directory...
>> defaultFile=fullfile('*') % no extension, directory again
defaultFile =
'*'
>> pwd % show where we are when run first...
ans =
'C:\Temp'
>> [f,d]=uiputfile('*.*','Save To',defaultFile)
f =
'NewName'
d =
'C:\Users\DPB\Documents\MATLAB\Work\'
>>
Again, works as expected; nothing really required except to call it and parse the result; branching if the user cancels and the return filename and folder are numeric instead of character vectors.
>> [f,d]=uiputfile('*.*','Save To',defaultFile)
f =
0
d =
0
> assert(~isnumeric(f),'User Canceled')
User Canceled
>>

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by