How to read the .txt file in matlab?

3 次查看(过去 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
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?
Salad Box
Salad Box 2018-3-1
Hi,
Thank you for your reply. I tried a few command in Text File but haven't found a suitable one.
One thing to notice is that although when I double-click on the .txt file and opened it, it looks something like shown above;
but when I double clicked it within Matlab, it looks something like shown below where it indicates that it might have some sort of format (many rows?)
All I need is the first 30 something characters from each row (begin with 'active', end with ':').
Where else can I check if the information on 'other specific format' is needed? Please can you advise?
I tried to treated it as a big string but somehow it didn't let me.

请先登录,再进行评论。

采纳的回答

Paul Shoemaker
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 个评论
Salad Box
Salad Box 2018-3-2
编辑:Walter Roberson 2018-3-2
Thank you Paul.
As many of you advised I did look at Text File page however I didn't find the answer there.
Both you and some others mentioned about using 'regexp' which I never heard of. Guess I have to spend some time to study text processing.
I also used the code below to solve this problem. Thanks for code sharing.
fileIn = fileread('active_info.txt');
pattern = 'active\.([a-f0-9]+):';
data = regexp(fileIn, pattern, 'tokens');
Salad Box
Salad Box 2018-3-2
Thank you Walter! Yes you are right. Fileread is the right one to use. Very much appreciate your answer. :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by