Read files from directory, load function causing error
5 次查看(过去 30 天)
显示 更早的评论
from the code below, when I run it and select the files I want, I get this error, I seem to be lost and cant figure out whats causing it> It seems to be reading letters of the file instead of the name of the file: ??? Error using ==> load Unable to read file G: No such file or directory.
G is the first letter of the file I selected.
The other problem is if i select more than one files i get this error:
??? Error using ==> fprintf Function is not defined for 'cell' inputs.
Error in ==>at line 34 fprintf('You have selected: %s\n\n',files2get)
Can someone help me?
when I change the load functions code to:
matrix = load(files2get{i,1},'%c%e%g%s%f'); i get this:
??? Cell contents reference from a non-cell array object.
Error in ==> twinleaf at 38 matrix = load(files2get{i,1},'%c%e%g%s%f');
Question=input('Do You Want To Select Files: y/n \n\n', 's')
Prompt=Question;
if strcmp(Prompt,'y')
fprintf('You selected: %s \n\n',Prompt)
fprintf('Please select the file type in the directory \n\n')
files2get = uigetfile('*.*','MultiSelect','on');
files2get={};
fprintf('You have selected: %s\n\n',files2get)
nfiles = size(files2get,1);
for i = 1:nfiles
matrix = load(files2get(i,1),'%c%e%g%s%f');
end elseif strcmp(Prompt,'n')
fprintf('You selected: %s\n\n GOODBYE',Prompt)
end
0 个评论
采纳的回答
Image Analyst
2014-4-18
Rather than say everything that's wrong with that, I'll just show you how to do it correctly, and in a more robust way:
promptMessage = sprintf('Do you want to select files?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Yes', 'No, quit', 'Yes');
if strcmpi(button, 'No, quit')
fprintf('You selected: %s\n\n GOODBYE\n',Prompt)
return;
end
fprintf('You selected: %s \n\n',Prompt)
fprintf('Please select the files in the dialog box\n\n')
[baseFileNames, folder] = uigetfile('*.*', 'Select a file', 'MultiSelect','on');
if isequal(baseFileNames,0)
% User clicked the No button.
fprintf('You selected: %s\n\n GOODBYE\n',Prompt)
return;
end
% User selected yes if they got here.
fullFileName = fullfile(folder, baseFileNames)
fprintf('You have selected:\n')
fprintf('%s\n',baseFileNames{:})
nfiles = length(baseFileNames)
for k = 1 : nfiles
% Get this fullfile name.
thisFileName = fullfile(folder, baseFileNames{k});
% Load it in to a structure.
fprintf('Loading %s\n', thisFileName);
matrix = load(thisFileName);
% Might need to do matrix = matrix.matrix if matrix is a structure instead of an array.
% Now process matrix before moving on to the next file.
end
Hopefully it's self explanatory. If not, ask.
10 个评论
Image Analyst
2014-4-21
Not sure what you changed, but whatever...as long as it's working now you're in good shape.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel Computing Fundamentals 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!