Pull out strings and its values from a text file.
Show older comments
Hi
Please find the attachment *.txt file. I want to analyze the whole text file .
Thanks
-Sriram
1 Comment
Walter Roberson
on 25 May 2019
Accepted Answer
More Answers (1)
Dimitar Georgiev
on 26 May 2019
0 votes
cell = readcell('filename.xlsx','Range','......');
stringname = '......';
variable = strcmp(stringname,cell);
12 Comments
Life is Wonderful
on 26 May 2019
Edited: Life is Wonderful
on 29 May 2019
Life is Wonderful
on 28 May 2019
Edited: Life is Wonderful
on 29 May 2019
Guillaume
on 29 May 2019
I want to parse the full file using textscan
Why the insistence on using textscan? The modern readtable, readcell, etc. can usually figure out the format for you and if not usually do it after being given a few hints. And they output something more useful than textscan.
One thing that you should never do is create numbered variables. Instead of embedding an index in the variable name, use proper indexing.
Walter Roberson
on 29 May 2019
Looking at the sample file, I would say it is too irregular for textscan or readtable to be useful. I would instead fileread() and use regexp() to pull it apart.
Guillaume
on 29 May 2019
Oh yes, I didn't look at the file, since the accepted answer use readcell to read an excel file. Any file that is similar to an excel file (i.e tabulated) can easily be read without using textscan.
Having now looked at the file, I would agree that readxxx would be completely unsuitable and textscan would struggle. Indeed regexp or a dedicated parser would be the way to go.
Life is Wonderful
on 30 May 2019
Guillaume
on 30 May 2019
Most of your requirements are requirements on how the code should be implemented instead of on what it should do. This is not how you design code. You first specify what result you want, then you use whichever implementation gives you these results efficiently.
Whether structures, cell arrays, cellfun, etc. are useful is unknown because you haven't specified what you want other than some sort name/value pair (what does pattern string and value refer to?) and a plot of something (what is the data?)
Life is Wonderful
on 30 May 2019
Edited: Life is Wonderful
on 30 May 2019
Guillaume
on 30 May 2019
Ok,so you want to parse each line of the file and split the lines into various components.
Once again, you're also giving an implementation. I'm not convinced that the structure you outline is a good idea, but that's not important right now.
The first thing you have to do, before we can even think how to implement it, is define exactly the parsing rules for the lines of the file. The start of the rule is going to be:
- extract all the characters up to the first space and decode that as time
- then, extract the characters up to the colon. That's the log source (I assume)
After that I'm not sure. It looks like the rule may vary according to the log source. If the log source is ANYTHING kernel, then the next step is
- Extrace the number between [] as the log value
Then it gets very murky, you get different types of messages after the [xxx] with different formatings. You will have to establish the rules for how these should be decoded.
If the log source is not the kernel, you get a completely different format of message. Again, you need to specify the rules for decoding these.
So, I'm afraid, the task is back onto you. You first need to define rules (there's going to be several due to the complex formatting of the lines) on how to split a line into various components. Only once you've done that can we think about writing the code to do it.
I suggest you continue this bullet point list:
For each line:
- extract the text up to the first space as the logtime
- then extract the text up to the colon as the logsource
- if logsource ends with kernel
- extract the number between the [] as logvalue
- ????
- if logsource is ????
- ????
- ????
- if ???
- ????
- ????
Life is Wonderful
on 30 May 2019
Guillaume
on 1 Jun 2019
As I wrote:
So, I'm afraid, the task is back onto you You first need to define rules (there's going to be several due to the complex formatting of the lines) on how to split a line into various components. Only once you've done that can we think about writing the code to do it
Life is Wonderful
on 6 Jun 2019
Categories
Find more on Text Data Preparation 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!