I prefer not to mix tasks. I first read my file, then I do the processing. It is generally a lot easier to remove parts of char arrays than to mess with fscanf to move the file pointer.
You can get my readfile function from the FEX. If you are using R2017a or later, you can also get it through the AddOn-manager. That will allow you to read a file to a cellstr, which you can then process with a simple loop. On R2020b or newer you can also use readlines instead.
The advantage of this setup is that every block of code has a clear purpose. Your current code has two jobs: reading the file, and while reading it, it should also be aware of the contents of the file and deal with that. Such a setup makes code confusing and difficult to debug and maintain.