Hi,
I have a many '.txt' files in a folder.
a.txt, b.txt, c.txt ......
Now I want to open each one of them one by one and print the following sentence in the 5th line from the beginning of each one them including the file name.
The sentence is :
a.txt > "I am in a file"
b.txt > "I am in b file"
these line should be printed on the 5th line from the file starting.
Can you please tell me the procedure

 Réponse acceptée

Image Analyst
Image Analyst le 8 Août 2021
And this snippet:
% Open the file for reading in text mode.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
lineCounter = 1;
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
% Read the next line.
textLine = fgetl(fileID);
lineCounter = lineCounter + 1;
end
% All done reading all lines, so close the file.
fclose(fileID);
Good luck.

3 commentaires

JAMMI ASHOK
JAMMI ASHOK le 8 Août 2021
Thank you. By the way, it's not a homwwork question.
I am trying to understand a code and see which functions are getting called.
For example:
getcl.py ->
def getcoord():
....................
....................
return
similarly I have 256 files in a folder. I don't know which sequence they are getting called.
So, I want to read each file as text - so that I can print at the 5 the line from the starting because generally
the function definitions will be starting at 4 th line.
print("I am in getcl")
So, If this statement gets printed in the 5th line, this will get printed on my console when program is running.
Now the program looks like:
getcl.py ->
def getcoord():
.print("I am in getcl")
...................
....................
return
I don't need the function name, just file name is sufficient.
I hope you understand
To find out what functions a file requires, you can do
[requiredFileList, toolboxList] = matlab.codetools.requiredFilesAndProducts(fullFileName);
Attached is my function wrapper for it.
JAMMI ASHOK
JAMMI ASHOK le 8 Août 2021
Thank you.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Centre d'aide et File Exchange

Produits

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by