Importing data from *.asc file(s)
80 views (last 30 days)
Show older comments
Hi,
In my day to day work, I log several data using CANalyzer to collect vehicle CAN network data and its a *.asc format. This log file contains all the CAN messages present on the network. But, I'd like to import the whole log file in to Matlab but would like to extract only specific rows. The rows can be identified by specific CAN ID.
For example, if the entire log file has a total of 1000 rows, it could be shared between two CAN IDs as 500 rows with CAN ID '222', and the rest of 500 rows with CAN ID '333'. What I'd like to achieve is, 'To load the entire log file but extract only the rows that contains CAN ID of '222'.
So, could some help me with any script file that would help me to solve this issue please.
Thanks
4 Comments
Jan
on 17 Apr 2013
Edited: Jan
on 17 Apr 2013
I cannot guess, what the CAN ID is in this text file. Perhaps it is the 2nd number with the integer value, perhaps you mean the last entry "ID = ..." in each line. It is not an efficient idea to let us guess.
Posting the text snippet as text would be much more useful, because we could experiment with the data. Posting the new information inside the original question would be a good method also, because this is the place where reader need the information.
Please explain, if the first 5 lines can be skipped in every case, or how the header can be identified securely.
Accepted Answer
Walter Roberson
on 17 Apr 2013
CANID = '1CF'
fid = fopen('YourFile.asc', 'rt');
for K = 1 : 7; fgetl(fid); end %skip header
CANpattern = ['^\S+\s+\d+\s+' CANID];
while true
thisline = fgetl(fid);
if ~ischar(thisline); break; end %end of file
if ~regexpi(thisline, CANpattern); next; end %line is wrong ID
... do something with thisthis line
end
fclose(fid);
2 Comments
Walter Roberson
on 18 Apr 2013
While I was taking a shower I realized that I had a minor bug in the pattern. It should be
CANpattern = ['^\S+\s+\d+\s+' CANID '\s'];
The extra \s allows the IDs 1 and 1C to be distinguished from 1CF .
Your loop is overwriting C each time. I suggest,
inlines = {};
while true
thisline = fgetl(fid);
if ~ischar(thisline); break; end %end of file
if ~regexpi(thisline, CANpattern); next; end %line is wrong ID
inlines{end+1} = thisline;
end
fclose(fid)
inlines - char(inlines); %textscan cannot deal with cellstr
fieldscell = textscan(inlines.', '%f%f%s%s%s%f%s%s%s%s%s%f%s%s%f%s%s%f');
disp(fields{1}) %first column
Using the .' on the character array is a trick... don't expect it to make perfect sense!
More Answers (1)
Mike Scannell
on 14 Feb 2023
This is an old question, but still comes up when searching about using Matlab to read CAN .asc files.
Since 2017, the Vehicle Network Toolbox has the function canSignalImport which can read this type of file and provides the ability to import only selected messages.
0 Comments
See Also
Categories
Find more on Data Import and Export 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!