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 CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by