How to read the .txt file in matlab?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Salad Box
am 28 Feb. 2018
Bearbeitet: Walter Roberson
am 2 Mär. 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 Kommentare
Geoff Hayes
am 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?
Akzeptierte Antwort
Paul Shoemaker
am 1 Mär. 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!