Pull out strings and its values from a text file.
显示 更早的评论
Hi
Please find the attachment *.txt file. I want to analyze the whole text file .
Thanks
-Sriram
1 个评论
Walter Roberson
2019-5-25
采纳的回答
更多回答(1 个)
Dimitar Georgiev
2019-5-26
0 个投票
cell = readcell('filename.xlsx','Range','......');
stringname = '......';
variable = strcmp(stringname,cell);
12 个评论
Life is Wonderful
2019-5-26
编辑:Life is Wonderful
2019-5-29
Life is Wonderful
2019-5-28
编辑:Life is Wonderful
2019-5-29
Guillaume
2019-5-29
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
2019-5-29
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
2019-5-29
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
2019-5-30
Guillaume
2019-5-30
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
2019-5-30
编辑:Life is Wonderful
2019-5-30
Guillaume
2019-5-30
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
2019-5-30
Guillaume
2019-6-1
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
2019-6-6
类别
在 帮助中心 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!