
fscanf read a specific line
18 views (last 30 days)
Show older comments

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
on 1 Jan 2023
Comment on "Answer" from yusuf moved here (could use the Move function, but then they'd be out of order):

Answers (1)
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);
0 Comments
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!