Converting a cell array to can message timetable
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I am trying to convert a cell array into can message timetable format ,
% Given cell array
canMessageCell = {'Timestamp 1546909042', 'Interface ''can0''', 'ID ''0B9''', 'Data [0,4,0,0,0,0,0,0]'};
% Extract the information from the cell array
timestampStr = canMessageCell{1};
idStr = canMessageCell{3};
dataStr = canMessageCell{4};
% Convert extracted strings to proper data types
timestamp = datetime(str2double(extractAfter(timestampStr, 'Timestamp ')), 'ConvertFrom', 'posixtime');
id = hex2dec(extractBetween(idStr, 'ID ''', ''''));
% Extract the data portion and convert it to a numeric array
dataChar = extractBetween(dataStr, 'Data [', ']');
data = str2num(dataChar{1}); % Convert cell array to character vector before using str2num
% Assume the CAN message is not an extended frame, not a remote frame, and has no error
isExtended = false;
isRemote = false;
hasError = false;
% Create a structure array with the expected field names
canMessageStruct = struct('TimeStamp', timestamp, 'ID', id, 'Data', {data}, 'Extended', isExtended, 'Remote', isRemote, 'Error', hasError);
% Convert the structure array to a cell array
canMessageCell = {canMessageStruct};
% Load the DBC file (replace with the actual path to your DBC file)
dbcFile = 'drivekit_kia_soul_ev.dbc';
db = canDatabase(dbcFile);
% Convert the cell array to a canMessageTimetable
msgTimetable = canMessageTimetable(canMessageCell, db);
% Display the result
disp(msgTimetable);
I am following this syntax for converting my cell array into CAN message timetable db = canDatabase('myDatabase.dbc')
msgTimetable = canMessageTimetable(canMsgs,db) but I am getting this error, Error using ()
Timetable row subscript must be a numeric, logical, or datetime array, or a timerange subscripter.
Error in canmessagetimetableconversion (line 33)
msgTimetable = canMessageTimetable(canMessageCell, db);
I dont know why because my timestamp is in datetime format as i converted it from posix format, my data is in uint8 format and I have created a struct for my timestamp, ID , Data , I have assumed Extended, Remote, Error to be false , if anyone can please help me in debugging this issue as i have been stuck on this , it would be greatly appreciated.
0 Commenti
Risposte (2)
Hassaan
il 20 Mag 2024
Initial implementation:
% Given cell array
canMessageCell = {'Timestamp 1546909042', 'Interface ''can0''', 'ID ''0B9''', 'Data [0,4,0,0,0,0,0,0]'};
% Extract the information from the cell array
timestampStr = canMessageCell{1};
idStr = canMessageCell{3};
dataStr = canMessageCell{4};
% Convert extracted strings to proper data types
timestamp = datetime(str2double(extractAfter(timestampStr, 'Timestamp ')), 'ConvertFrom', 'posixtime');
id = hex2dec(extractBetween(idStr, 'ID ''', ''''));
% Extract the data portion and convert it to a numeric array
dataChar = extractBetween(dataStr, 'Data [', ']');
data = uint8(str2num(dataChar{1})); % Convert cell array to character vector before using str2num
% Assume the CAN message is not an extended frame, not a remote frame, and has no error
isExtended = false;
isRemote = false;
hasError = false;
% Create a structure array with the expected field names
canMessageStruct = struct('Timestamp', timestamp, 'ID', id, 'Data', {data}, 'Extended', isExtended, 'Remote', isRemote, 'Error', hasError);
% Convert the structure to a cell array of structures
canMessageCellArray = {canMessageStruct};
% Load the DBC file (replace with the actual path to your DBC file)
dbcFile = 'drivekit_kia_soul_ev.dbc';
db = canDatabase(dbcFile);
% Convert the cell array to a canMessageTimetable
msgTimetable = canMessageTimetable(canMessageCellArray, db);
% Display the result
disp(msgTimetable);
Peter Perkins
il 29 Mag 2024
I don't know much about the Vehicle Network Toolbox, but the following suggests that your code has two problems:
...
>> canMessageStruct = struct('TimeStamp', timestamp, 'ID', id, 'Data', {data}, 'Extended', isExtended, 'Remote', isRemote, 'Error', hasError);
>> canMessageCell = {canMessageStruct};
>> msgTimetable = canMessageTimetable(canMessageCell)
Error using canMessageTimetable
Expected messages to be one of these types:
timetable, can.Message, struct
Instead its type was cell.
Error in
canMessageTimetable (line 57)
validateattributes(messages, {'timetable', 'can.Message', 'struct'}, {}, 'canMessageTimetable', 'messages');^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> msgTimetable = canMessageTimetable(canMessageStruct)
Unrecognized field name "Timestamp".
Error in vnt.internal.convertMessageStructsToTimetable (line 13)
if isdatetime([msgStructs.Timestamp]) ^^^^^^^^^^^^^^^^^^^^
Error in
canMessageTimetable (line 169)
messageTimetable = vnt.internal.convertMessageStructsToTimetable(messageStructs, database); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This seems to work:
>> canMessageStruct = struct('Timestamp', timestamp, 'ID', id, 'Data', {data}, 'Extended', isExtended, 'Remote', isRemote, 'Error', hasError);
>> msgTimetable = canMessageTimetable(canMessageStruct)
msgTimetable =
1×8 timetable
Time ID Extended Name Data Length Signals Error Remote
____________________ ___ ________ __________ ___________________ ______ ____________ _____ ______
08-Jan-2019 00:57:22 185 false {0×0 char} {[0 4 0 0 0 0 0 0]} 8 {0×0 struct} false false
0 Commenti
Vedere anche
Categorie
Scopri di più su Data Type Conversion in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!