Clear Filters
Clear Filters

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

20 views (last 30 days)
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

Accepted Answer

Ludo Houben
Ludo Houben on 4 Jul 2024 at 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 Comment
Stephen23
Stephen23 on 4 Jul 2024 at 13:04
Edited: Stephen23 on 4 Jul 2024 at 13:25
Simpler:
if isequal(files,0)
return
end
files = cellstr(files);

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!