MATLAB Answers

Find string in the special file

2 views (last 30 days)
Farzad Fakhraeiroudsari
Farzad Fakhraeiroudsari on 7 Sep 2020
Commented: Rik on 8 Sep 2020
Hello Everyone.
I'm trying to export scenario names from a file that is not a txt file but I can see the names if I open it in the notepad. Here is a sample of the data in the file
O(1}(2019-12-4 12:12:43Szenario 7: OS - Haventransport - Copy has been deleted.>'1['2019-12-4 12:12:43Szenario 6: OS - Copy has been deleted.:&1S&2019-12-3 16:35:12Modifikation 10: has been created.N%/}%2019-12-3 11:21:2Szenario 8: AS - Haventransport - Copy has been created.N$/}$2019-12-3 11:21:2Szenario 7: OS - Haventransport - Copy has been created.="/["2019-12-3 11:21:2Szenario 5: AS - Copy has been created.N!/}!2019-12-3 11:20:7Szenario 5: AS - Haventransport - Copy has been deleted.9 1Q 2019-12-3 11:19:48Modifikation 9: has been created.O­1}­2019-12-3 11:17:59Szenario 5: AS - Haventransport - Copy has been created.:‑3Q‑2019-11-26 11:48:22Modifikation 8: has been created.91Q2019-11-26 9:26:42Modifikation 7: has been created.S12019-11-26 …..
The name is written after Szenario and end by (“has been created.”). for example the first one is Scenario 8 and the name is “AS - Haventransport – Copy”. I want to export the Scenario number and It’s name. since the file is not text and the sequence of characters is not consistent, I cannot use regexp or I can but I don’t know how.
I appreciate any suggestion or help.
Best regards

Answers (2)

Rik on 7 Sep 2020
If notepad can open it and you see plain text, why do conclude it is not a text file? Even if there are some special characters in there you can just ignore them.
I suggest you use fileread to read the file to a single long char array. You can then remove any invalid chars if you like, after which you can use regexp or any other method to find this pattern.
If you need help, attach an example file to your question (simply change the extension to txt or zip the file). You should also mention your release if you aren't using the latest release.
Rik on 8 Sep 2020
Running the code below with my sqlite3 function (on Octave, because for some reason the interface refuses to work on Windows 8 + R2020a and I don't have time for thorough debugging now) returned a cell array:
fn='C:\Users\Rik\Desktop\R2_AS - Copy.db';

Sign in to comment.

Jan on 7 Sep 2020
Edited: Jan on 7 Sep 2020
S = fileread('R2_AS - Copy.txt');
i1 = [strfind(S, 'Szenario'), Inf];
i2 = strfind(S, 'has been created.');
width = numel('has been created.') - 1;
Result = cell(1, numel(i1));
iResult = 0;
for k = 1:numel(i1) - 1
ini = i1(k);
fin = i2(find(i2 > ini, 1));
if ~isempty(fin) && fin < i1(k + 1)
iResult = iResult + 1;
Result{iResult} = S(ini:fin + width);
Result = Result(1:iResult);
You will get some strange results as:
'Szenario 9: OS - uitbereden - Copy has been deleted.:/S —2020-2-7 12:26:34Modifikation 28: has been created.'
The results are not unique also for a specific Szenario number - some examples:
'Szenario 9: AS - Copy has been created.' }
'Szenario 9: OS - uitbereden - Copy has been deleted.:/S —2020-2-7 12:26:34Modifikation 28: has been created.' }
'Szenario 9: AS - Uitbereden R2 - Copy has been created.'
So what do you want to extract exactly?Where does the wanted "name" end?
  1 Comment
Farzad Fakhraeiroudsari
Farzad Fakhraeiroudsari on 8 Sep 2020
Thanks Jan. This is from a traffic planning software and the file came from it's database where it's mentioned the user create or delete a scenario from the database. As you mentioned for scenario 9 several names were mentioned but I'm interested in the last occurance, AS - Uitbereden R2 - Copy for Scenario 9 for instance.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!