Borrar filtros
Borrar filtros

How to read the .txt file in matlab?

3 visualizaciones (últimos 30 días)
Salad Box
Salad Box el 28 de Feb. de 2018
Editada: Walter Roberson el 2 de Mzo. de 2018
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 comentarios
Geoff Hayes
Geoff Hayes el 28 de Feb. de 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?
Salad Box
Salad Box el 1 de Mzo. de 2018
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.

Iniciar sesión para comentar.

Respuesta aceptada

Paul Shoemaker
Paul Shoemaker el 1 de Mzo. de 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 comentarios
Salad Box
Salad Box el 2 de Mzo. de 2018
Editada: Walter Roberson el 2 de Mzo. de 2018
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 el 2 de Mzo. de 2018
Thank you Walter! Yes you are right. Fileread is the right one to use. Very much appreciate your answer. :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by