Combine reading .txt
Info
This question is closed. Reopen it to edit or answer.
Show older comments
I have a coding like this:
% Specify the folder where the files live.
myFolder = 'D:\Home\MultiLinierRegression\OutputCalc';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.txt'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
temp = importdata(fullFileName);
%imshow(imageArray); % Display image.
% drawnow; % Force display to update immediately.
Y=temp(:,1); X=temp(:,2:end);
X=[X temp(:,2:end)];
end
But I don't why my coding only read two files. Actually in my folder I have 18 files.
Thanks
7 Comments
Walter Roberson
on 15 Nov 2019
Are you getting any error message?
I wonder if the importdata() is failing, or if the importdata() is returning something non-numeric. importdata() has a habit of changing the datatype returned depending on the content of the file, which is why I do not use it: if I know the expected format of the file then I use an appropriate routine for that kind of file.
Skydriver
on 15 Nov 2019
Walter Roberson
on 15 Nov 2019
What shows up for
length(theFiles)
{theFiles.name}
Skydriver
on 16 Nov 2019
Walter Roberson
on 16 Nov 2019
.txt is a file extension, not a format. format is the details of how the information is laid out.
per isakson
on 16 Nov 2019
Add the line
reshape( {theFiles.name}, [],1 )
before the for-loop. What does it print to Command Window?
Skydriver
on 18 Nov 2019
Answers (0)
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!