Importing data from *.asc file(s)
조회 수: 72 (최근 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에서 Standard File Formats에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
