How can I use multiple files from uiget with importdata?

7 views (last 30 days)
I'm trying to load in several files from a folder using uiget so that it is user friendly for other folks. Right now I have the following code:
Import data
[filename,pathname] = uigetfile({'*.*', 'All Files (*.*)'},'Pick a file','MultiSelect','on');
delimiterIn = ',';
A = importdata(filename, delimiterIn);
When I use this I get the following error:
Error using importdata (line 136)
Unable to open file.
Error in LoadSQMData (line 16)
A = importdata(filename, delimiterIn);
It seems that my issue may be because the multiple files load in as a cell, but when it is a single file it loads in as a character struct. I'm able to figure out how to change the cell to a char, but I can't figure how to use importdata on this alteration. Any thoughts on how I could get this work?
Thanks in advance!

Answers (1)

ES
ES on 10 Jun 2014
You have to use a for loop to load files one by one.
delimiterIn = ',';
[filename,pathname,~] = uigetfile({'*.*', 'All Files (*.*)'},'Pick a file','MultiSelect','on');
if isequal(pathname,0)
% No file Selected
disp('No file Selected!');
return;
elseif iscell(filename)
% Multiple Files Selected
FilesCount=length(filename);
for loop1=1:FilesCount
sFileName=filename{loop1};
if ~isequal(sFileName,0)
sFullFileName=fullfile(pathname,sFileName);
A = importdata(sFullFileName, delimiterIn);
end
end
else
% One File Selected
sFileName=filename;
sFullFileName=fullfile(pathname,sFileName);
[~,sModelName,~] = fileparts(sModelName);
A = importdata(sFullFileName, delimiterIn);
end

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!