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')
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');
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)

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)
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)
% User selected yes if they got here.
fullFileName = fullfile(folder, baseFileNames)
fprintf('You have selected:\n')
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.
Hopefully it's self explanatory. If not, ask.
Image Analyst
Image Analyst on 21 Apr 2014
Not sure what you changed, but long as it's working now you're in good shape.

Sign in to comment.

More Answers (0)


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!