fscanf read a specific line

11 views (last 30 days)
yusuf
yusuf on 30 Dec 2022
Commented: Voss on 1 Jan 2023
Hello, in matlab 1-2-3... I have prepared 10 files of 100 numbers, each containing 10 numbers. Now, what I want to do is the 1st line from the 1st file, the 2nd line from the 2nd file, that is n. from file n. I want to get the row. Can you help with the code?
  2 Comments
Voss
Voss on 1 Jan 2023
"Answer" from yusuf moved here (couldn't use the Move function for some reason):
Voss
Voss on 1 Jan 2023
Comment on "Answer" from yusuf moved here (could use the Move function, but then they'd be out of order):

Sign in to comment.

Answers (1)

Voss
Voss on 1 Jan 2023
Here's an example using three files:
n_files = numel(dir('ders*.txt')); % store the number .txt files whose names start with "ders" in the working directory (in this case, 3)
result = zeros(1,n_files); % initialize the resulting row vector
for ii = 1:n_files
dosya = fopen("ders"+ii+".txt","r");
temp = fscanf(dosya,"%d",Inf); % store all numbers from the ii-th file in the temporary variable "temp"
result(ii) = temp(ii); % store the ii-th number in "temp" as the ii-th element of the result vector
fclose(dosya);
end
disp(result);
1 12 23

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!