importing a series of text file and save them into a array

조회 수: 2 (최근 30일)
Sunghwan Kim
Sunghwan Kim 2022년 6월 29일
답변: Prateekshya 2023년 9월 6일
I have 54 text files sequatially named as follow.
2021.01.19_KBSI_CCM_Sa 3(Alq3-TAPC)_P_Bi3_200um_Ar_z-ROI#1.TXT
...
2021.01.19_KBSI_CCM_Sa 3(Alq3-TAPC)_P_Bi3_200um_Ar_z-ROI#54.TXT
I want to import 50 text files and save the files into a array such as "Alq3_TAPC{1} .... Alq3_TAPC{54}" . Can you let me know easier way of doing it? Thanks.
  댓글 수: 1
Jan
Jan 2022년 6월 29일
What das "naming a text file" mean? Do you want to modifiy the file name? If so, what is the reason to import them before? Please edit the question and elaborate the details.

댓글을 달려면 로그인하십시오.

답변 (1개)

Prateekshya
Prateekshya 2023년 9월 6일
Hi Sunghwan,
As per my understanding you are trying to read a set of files following a specific naming convention. I assume that the files contain strings which need to be stored in "Alq3_TAPC" array sequentially. You can try the following code for the same.
fileCount = 54; % Total number of files
Alq3_TAPC = cell(1,fileCount); % Cell array to store the contents of the files
for count = 1 : fileCount % For each file
fileID = fopen(sprintf("2021.01.19_KBSI_CCM_Sa 3(Alq3-TAPC)_P_Bi3_200um_Ar_z-ROI#%d.txt",count), ...
"r"); % Generate the file name and open it in read mode
formatSpecifier = '%s';
Alq3_TAPC{count} = fscanf(fileID,formatSpecifier); % Read the content and store in the corresponding index
end
For data types other than string you can modify the "formatSpecifier" variable. For more information please go through: https://in.mathworks.com/help/matlab/ref/fscanf.html
I hope this helps!

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by