How to import files from folder

7 ビュー (過去 30 日間)
ABhay
ABhay 2012 年 1 月 11 日
回答済み: nick 2025 年 4 月 3 日
i have stored .txt files in a folder. i want to process these data files using a .m file such that the processing of all text files available in folder should be done sequentially and processed data files should be stored under a different name in the same folder. how to import/export data from all text files using a loop. i used following code for importing files but it was unsuccesful. Please help. d=dir('directory name'); a=length(d); for i =1:a b(i)=d(i).name end
  1 件のコメント
Chandra Kurniawan
Chandra Kurniawan 2012 年 1 月 11 日
How do you format your txt files?
Can you show me one of them?

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

回答 (1 件)

nick
nick 2025 年 4 月 3 日
Hello Abhay,
When looping through the files shown by 'dir' command, you can follow these steps:
Open and Read Each File:
  • Open the file for reading using fopen.
  • Use 'fread' to read the file's contents into a variable.
Process the Data:
  • Perform any necessary processing on the data.
Create a New Filename:
  • Generate a new filename for the processed data, appending a suffix like _processed to the original filename.
Write the Processed Data:
  • Open a new file for writing using 'fopen'.
  • Write the processed data to this new file using 'fwrite'.
  • Check if the file opened successfully for writing.
Close Files:
  • Ensure all files are properly closed after reading and writing to prevent resource leaks.
You can refer to the following documentation to know more about the following functions by executing the corresponding code in MATLAB Command Window:
% For fopen
doc fopen
% For fread
doc fread
% For fwrite
doc fwrite
% For fclose
doc fclose
Hope this helps.

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by