I have a text file with two coloumns for a certain amount of rows. The coloumns are then divided from a text line that start with #, how can I load only the data by removing the # line?
Hmmmm....I don't see the release information on the Q?; I do use the compact format, but I'd think it still should show it if user specified it. I'll have to open another window and see if the alternate....oh! I see; it's over there in the RH column with all that other stuff I never pay attention to, not part of the Q? itself. I'll have to try to remember to go look, but nobody else caught it, either, including the MATHWORKS employee....so it clearly isn't in the most suitable location.
Anyway, did you see my followup Answer given the release? readtable should solve your problem as a one-liner.
With the new information of R2018 that predates all the answers initially given, the easiest high-level toolset will be to use readtable; it goes back to R2013
Alternatively, as mentioned in earlier sidebar conversation, reverting to the venerable textread would probably be my second choice even though it is now deprecated.
See <readmatrix> for details. Also readtable supports the same option if a table were desired instead of the array; also particularly if the file does have variable names as the first record.
Lots of good answers in this thread! I was operating under the assumption that this is text data, not just a .txt file. If the data is numeric, @dpb's response is a solid plan of action. @Image Analyst's response is also great if you're looking to parse each line individually.
However for some bizarre reason, readlines does not??? Why is anybody's guess; it wasn't introduced until after the other family had already been; why it is neutered in comparion seems as though the internal review/quality control/user interface teams weren't doing their job. Why is it the red-haired stepchild?
@dpb I do not have an explanation for why readlines does not take 'CommentStyle' as an input argument. textscan does though. Ultimately, I think we will need to wait for @Christian_Avanzato's response to see which function best suits their needs.
I have passed your feedback on readlines on to our development team who may implement this change in the future.
Granted the venerable textscan supports 'CommentStyle', but one has to know the "trick" to set 'delimiter','\n','whitespace','' in order for it to not split lines on whitespace; something not many new(ish) users are going to know -- and the example for doing that with it seems to come and go; in the current incarnation I have installed (R2021b), it isn't; one has to have enough time in grade to know about even more venerable textread to find the example...
@dpb Update from development on the readlines function: "The other functions mentioned are "formatted" text function that expect some structure of the data. readlines is meant simply to read the lines in the file. Its interface is kept minimal on purpose."
That makes no sense at all to me..."make things as simple as possible, but not too simple".
I suggest the choice should be the user's rather than the developer deciding they shouldn't need to do that and that the request for the additional option be retained.
While I'll agree not all the options available with the other members of the family are appicable to the purpose of readlines, I will argue to the end that skipping whole lines based on comment style is a line-reading functionality (as the subject question illustrates) and should be available.