Read files from directory, load function causing error

1 view (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 18 Apr 2014
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 Comments
Mini Me
Mini Me on 21 Apr 2014
seems to work this 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: No %s\n\n GOODBYE\n', promptMessage)
return;
end
fprintf('You selected: %s \n\n',promptMessage)
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',promptMessage)
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)
M=size(baseFileNames)
for k = 1 : nfiles % Get the file date
fileInfo = dir(fullFileName);
% Show the file time and date.
fprintf('Date of %s = %s\n', fullFileName, fileInfo.date);
% Get this fullfile name.
thisFileName = fullfile(folder, baseFileNames);
% 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
Image Analyst
Image Analyst on 21 Apr 2014
Not sure what you changed, but whatever...as long as it's working now you're in good shape.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!