Borrar filtros
Borrar filtros

uigetfile, how to get 1x1 cell array when selecting 1 file

1 visualización (últimos 30 días)
Hi All
I use 'uigetfile' for selecting files by the user. This can be multiple files or only 1. When selecting multiple files, then the output is a 1x<number of selected files> cell (array). This works fine and my code (FOR LOOP) handles this beautifully. Then I tested the function with selecting only 1 file, but instead of spitting out an 1x1 cell (array), it spits out a 'char' of the file name. Causing the rest of the FOR LOOP to crash. How do I get the function to spit out a 1x1 cell (array) with the file name?
This is what I use at this point:
[files,folder] = uigetfile({'C:\Logs\NewElectronics\*.*'},'Select log data file', 'MultiSelect','on');
After selecting I check the 'files' variable and I try to convert it to a cell array. So let's say the file is called 'Probe.txt' then
% File checking
if isequal(files,0) % Is at least one file selected
return;
elseif ~iscell(files) % Is only one file selected
files = num2cell(files); % Convert to cellArray
end
This results in
{P}{r}{o}{b}{e}{.}{t}{x}{t} 1x9 cell
When I add 'join'
% File checking
if isequal(files,0) % Is at least one file selected
return;
elseif ~iscell(files) % Is only one file selected
files = join(num2cell(files)); % Convert to cellArray
end
This results in
{P r o b e . t x t} 1x1 cell
But I need
{Probe.txt} 1x1 cell
Thanks for any help in the correct direction.
Regards
Ludo

Respuesta aceptada

Ludo Houben
Ludo Houben el 4 de Jul. de 2024
This did the trick
% File checking
if isequal(files,0) % Is at least one file selected
return;
elseif ~iscell(files) % Is only one file selected
files = cellstr(files); % Convert to cellArray
end
  1 comentario
Stephen23
Stephen23 el 4 de Jul. de 2024
Editada: Stephen23 el 4 de Jul. de 2024
Simpler:
if isequal(files,0)
return
end
files = cellstr(files);

Iniciar sesión para comentar.

Más respuestas (0)

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