フィルターのクリア

.txt files?

4 ビュー (過去 30 日間)
Khan Muhammad Adeel Khan
Khan Muhammad Adeel Khan 2022 年 10 月 20 日
I have 43 text files in a folder. How can I read all files in matlab?
  4 件のコメント
David Hill
David Hill 2022 年 10 月 20 日
If the sizes are the same, I would you readmatrix and store them all in a matrix. Otherwise if the sizes are different, use readmatrix and store then in a cellarray.
Khan Muhammad Adeel Khan
Khan Muhammad Adeel Khan 2022 年 10 月 20 日
Many thanks David

サインインしてコメントする。

採用された回答

David Hill
David Hill 2022 年 10 月 20 日
theFiles = dir('*.txt');
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
A(:,k)=readmatrix(baseFileName);%for same sizes
B{k}=readmatrix(baseFileName);%for different sizes
end

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 10 月 20 日
We get this question several times a week, so see the FAQ for code snippets:
% Specify the folder where the files live.
myFolder = 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
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(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an array with readmatrix() or importdata()
% or whatever function you want.
allLines = readlines(fullFileName);
end
Another way to do it is in the FAQ

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by