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

4 ビュー (過去 30 日間)
Ludo Houben
Ludo Houben 2024 年 7 月 4 日
編集済み: Stephen23 2024 年 7 月 4 日
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

採用された回答

Ludo Houben
Ludo Houben 2024 年 7 月 4 日
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 件のコメント
Stephen23
Stephen23 2024 年 7 月 4 日
編集済み: Stephen23 2024 年 7 月 4 日
Simpler:
if isequal(files,0)
return
end
files = cellstr(files);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by