Why does uigetfile not ask for another input in a for loop

2 visualizaciones (últimos 30 días)
I have a small for loop in which I try to load several input files and store them as well as some meta data in various variables. The first iteration runs fine, but on the second loop iteration the code skips over the uigetfile command and instead of letting me select another file, it just fills the next entry in my cell aray with [0].
I am using Matlab 2024b at the moment but the script was originally written in Matlab 2022b, where it was (and still is) working perfectly fine. The loop is incredibly simple:
for i = 1:4
[info.name{i}, info.path{i}] = uigetfile('', ['File Nr. ', num2str(i), ' auswaehlen.']);
info.vnr{i} = info.name{i}(1:5);
if info.vnr{i} == 0
error('Kein valides Inputfile ausgewählt')
end
end
Has the syntax for uigetfile changed? I have recently added the new Desktop App that allows me to use dark mode, but that shouldn't change how code works, right?
  2 comentarios
Stephen23
Stephen23 el 10 de Abr. de 2025
Editada: Stephen23 el 10 de Abr. de 2025
It worked for me when I tried it on R2024b.
The code does have some (latent) bugs though:
  • if the user cancels or closes the dialog box then the indexing info.name{i}(1:5); will throw an error.
  • The code if info.vnr{i} == 0 will only be considered true (and thus the code within the IF executed) if the first five characters of the filename are null characters, which is very very unlikely in any filename.
I doubt that that code has ever actually done what its author intended. Probably they intended something like this:
for k = 1:4
p = sprintf('File Nr. %u auswaehlen.',k);
[info.name{k}, info.path{k}] = uigetfile('',p);
assert(~isequal(info.name{k},0),'Kein valides Inputfile ausgewählt')
info.vnr{k} = info.name{k}(1:5);
end
Jan Sailer
Jan Sailer el 10 de Abr. de 2025
The code had already been in use and worked just fine in Matlab 2022b.
I have tried using your suggestion and it causes the same issue on the second loop:
Error using assert
Kein valides Inputfile ausgewählt
Error in
XXX (line 45)
assert(~isequal(info.name{k},0),'Kein valides Inputfile ausgewählt')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Iniciar sesión para comentar.

Respuesta aceptada

Jan Sailer
Jan Sailer el 10 de Abr. de 2025
I have found the source of the Problem. I was using the new Matlab Desktop App (I thought it was only a UI change) and as soon as I went back to the original Matlab the code ran just fine. Both my original code and the suggestion made by the commenter. I guess the New Desktop just isn't ready for use yet.

Más respuestas (0)

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by