How to read the .txt file in matlab?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Geoff Hayes
2018-2-28
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?
采纳的回答
Paul Shoemaker
2018-3-1
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!