Reading text file with variable name

조회 수: 3 (최근 30일)
Abdulaziz Abutunis
Abdulaziz Abutunis 2022년 6월 17일
편집: Voss 2022년 6월 17일
Dear all
I wanted to read a text file with variable names in a loop. file names are 1.txt, 2.txt, 3.txt, and 4.txt. The files will be called during iteration repeatedly. I have an array that has the pattern of how the files should be called say: file_number=[1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 ..]. I wonder if there is a way to call the files following this pattern.
Thanks

채택된 답변

Voss
Voss 2022년 6월 17일
One way, using file_number in the file name:
file_number=[1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4];
for ii = 1:numel(file_number)
current_file_name = sprintf('%d.txt',file_number(ii))
% read the file current_file_name
end
current_file_name = '1.txt'
current_file_name = '1.txt'
current_file_name = '1.txt'
current_file_name = '2.txt'
current_file_name = '2.txt'
current_file_name = '2.txt'
current_file_name = '3.txt'
current_file_name = '3.txt'
current_file_name = '3.txt'
current_file_name = '4.txt'
current_file_name = '4.txt'
current_file_name = '4.txt'
current_file_name = '1.txt'
current_file_name = '1.txt'
current_file_name = '1.txt'
current_file_name = '2.txt'
current_file_name = '2.txt'
current_file_name = '2.txt'
current_file_name = '3.txt'
current_file_name = '3.txt'
current_file_name = '3.txt'
current_file_name = '4.txt'
current_file_name = '4.txt'
current_file_name = '4.txt'
Another way, which works for any set of file names:
file_number=[1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4];
file_names = {'a_file.txt' 'another_file.txt' 'yet_another_file.txt' 'you_guessed_it_another_file.txt'};
for ii = 1:numel(file_number)
current_file_name = sprintf('%s',file_names{file_number(ii)})
% read the file current_file_name
end
current_file_name = 'a_file.txt'
current_file_name = 'a_file.txt'
current_file_name = 'a_file.txt'
current_file_name = 'another_file.txt'
current_file_name = 'another_file.txt'
current_file_name = 'another_file.txt'
current_file_name = 'yet_another_file.txt'
current_file_name = 'yet_another_file.txt'
current_file_name = 'yet_another_file.txt'
current_file_name = 'you_guessed_it_another_file.txt'
current_file_name = 'you_guessed_it_another_file.txt'
current_file_name = 'you_guessed_it_another_file.txt'
current_file_name = 'a_file.txt'
current_file_name = 'a_file.txt'
current_file_name = 'a_file.txt'
current_file_name = 'another_file.txt'
current_file_name = 'another_file.txt'
current_file_name = 'another_file.txt'
current_file_name = 'yet_another_file.txt'
current_file_name = 'yet_another_file.txt'
current_file_name = 'yet_another_file.txt'
current_file_name = 'you_guessed_it_another_file.txt'
current_file_name = 'you_guessed_it_another_file.txt'
current_file_name = 'you_guessed_it_another_file.txt'
  댓글 수: 2
Abdulaziz Abutunis
Abdulaziz Abutunis 2022년 6월 17일
Thank you very much
I found this works also
[num2str(file_number(i)),'.txt']
Voss
Voss 2022년 6월 17일
편집: Voss 2022년 6월 17일
You're welcome! That's right, num2str also works!
As an alternative to all of these approaches, which create one file name at a time, it may be convenient to create all the file names at once, before the loop:
file_number=[1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4];
all_file_names = sprintfc('%d.txt',file_number)
all_file_names = 1×24 cell array
{'1.txt'} {'1.txt'} {'1.txt'} {'2.txt'} {'2.txt'} {'2.txt'} {'3.txt'} {'3.txt'} {'3.txt'} {'4.txt'} {'4.txt'} {'4.txt'} {'1.txt'} {'1.txt'} {'1.txt'} {'2.txt'} {'2.txt'} {'2.txt'} {'3.txt'} {'3.txt'} {'3.txt'} {'4.txt'} {'4.txt'} {'4.txt'}
for ii = 1:numel(file_number)
current_file_name = all_file_names{ii}
end
current_file_name = '1.txt'
current_file_name = '1.txt'
current_file_name = '1.txt'
current_file_name = '2.txt'
current_file_name = '2.txt'
current_file_name = '2.txt'
current_file_name = '3.txt'
current_file_name = '3.txt'
current_file_name = '3.txt'
current_file_name = '4.txt'
current_file_name = '4.txt'
current_file_name = '4.txt'
current_file_name = '1.txt'
current_file_name = '1.txt'
current_file_name = '1.txt'
current_file_name = '2.txt'
current_file_name = '2.txt'
current_file_name = '2.txt'
current_file_name = '3.txt'
current_file_name = '3.txt'
current_file_name = '3.txt'
current_file_name = '4.txt'
current_file_name = '4.txt'
current_file_name = '4.txt'
file_number=[1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4];
file_names = {'a_file.txt' 'another_file.txt' 'yet_another_file.txt' 'you_guessed_it_another_file.txt'};
all_file_names = file_names(file_number)
all_file_names = 1×24 cell array
{'a_file.txt'} {'a_file.txt'} {'a_file.txt'} {'another_file.txt'} {'another_file.txt'} {'another_file.txt'} {'yet_another_file.txt'} {'yet_another_file.txt'} {'yet_another_file.txt'} {'you_guessed_it_another_file.txt'} {'you_guessed_it_another_file.txt'} {'you_guessed_it_another_file.txt'} {'a_file.txt'} {'a_file.txt'} {'a_file.txt'} {'another_file.txt'} {'another_file.txt'} {'another_file.txt'} {'yet_another_file.txt'} {'yet_another_file.txt'} {'yet_another_file.txt'} {'you_guessed_it_another_file.txt'} {'you_guessed_it_another_file.txt'} {'you_guessed_it_another_file.txt'}
for ii = 1:numel(file_number)
current_file_name = all_file_names{ii}
end
current_file_name = 'a_file.txt'
current_file_name = 'a_file.txt'
current_file_name = 'a_file.txt'
current_file_name = 'another_file.txt'
current_file_name = 'another_file.txt'
current_file_name = 'another_file.txt'
current_file_name = 'yet_another_file.txt'
current_file_name = 'yet_another_file.txt'
current_file_name = 'yet_another_file.txt'
current_file_name = 'you_guessed_it_another_file.txt'
current_file_name = 'you_guessed_it_another_file.txt'
current_file_name = 'you_guessed_it_another_file.txt'
current_file_name = 'a_file.txt'
current_file_name = 'a_file.txt'
current_file_name = 'a_file.txt'
current_file_name = 'another_file.txt'
current_file_name = 'another_file.txt'
current_file_name = 'another_file.txt'
current_file_name = 'yet_another_file.txt'
current_file_name = 'yet_another_file.txt'
current_file_name = 'yet_another_file.txt'
current_file_name = 'you_guessed_it_another_file.txt'
current_file_name = 'you_guessed_it_another_file.txt'
current_file_name = 'you_guessed_it_another_file.txt'

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by