Effacer les filtres
Effacer les filtres

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

20 vues (au cours des 30 derniers jours)
Ludo Houben
Ludo Houben le 4 Juil 2024 à 12:44
Modifié(e) : Stephen23 le 4 Juil 2024 à 13:25
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

Réponse acceptée

Ludo Houben
Ludo Houben le 4 Juil 2024 à 12:51
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 commentaire
Stephen23
Stephen23 le 4 Juil 2024 à 13:04
Modifié(e) : Stephen23 le 4 Juil 2024 à 13:25
Simpler:
if isequal(files,0)
return
end
files = cellstr(files);

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by