How to read the .txt file in matlab?
4 views (last 30 days)
Show older comments
Hi,
I have a .txt file look something like below.
I will need to read it in matlab and find certain strings (shown in the red boxes).
My question is how can i read it into matlab before I can use some sort of string find.
Please can you help...
2 Comments
Geoff Hayes
on 28 Feb 2018
You may want to start with Text Files to get an idea of the options available to you to read in this file. Are there any line breaks in this file or is it just a jumble like the above? Or is there any other specific format?
Accepted Answer
Paul Shoemaker
on 1 Mar 2018
I concur with Geoff on use of the support page for Text Files to understand which function to use for your case. Once you have it in Matlab as a string, character array, or cell, I encourage the use of regexp to parse through the content.
For example, once the text file is read in as a variable, called "myString" for the purposes of this example, you can do as follows:
myTokens = regexp(myString,'active\.(.*?\:)','tokens');
If you don't want to include the trailing ":" then just move "\:" outside of the parenthetical portion, like so:
myTokens = regexp(myString,'active\.(.*?)\:','tokens');
Note that you may have to unpack the result of regexp above because the content you're seeking may be nested in a few cell layers, depending on what function you use to read the text file. Just keep doing the following until you get to what you want, like so:
myTokens = myTokens{:}; % Unpack from cell array. Repeat this line as much as necessary to get to desired content.
Paul Shoemaker
3 Comments
More Answers (0)
See Also
Categories
Find more on Environment and Settings 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!