Importing data from *.asc file(s)
82 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2013-4-17
编辑:Jan
2013-4-17
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.
采纳的回答
Walter Roberson
2013-4-17
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 个评论
Walter Roberson
2013-4-18
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!
更多回答(1 个)
Mike Scannell
2023-2-14
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!