How can I use multiple files from uiget with importdata?

조회 수: 19 (최근 30일)
Will Roddy
Will Roddy 2014년 6월 10일
답변: ES 2014년 6월 10일
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!

답변 (1개)

ES
ES 2014년 6월 10일
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

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by