Cell2mat not working for unequal length cell data/array

12 vues (au cours des 30 derniers jours)
Logesh Velusamy
Logesh Velusamy le 17 Jan 2020
Commenté : Logesh Velusamy le 20 Jan 2020
Hello,
I am trying to process and plot the CAN message data in Matlab. To do this I am using the following code to import data from *.asc file into Matlab workspace.
canMsgs = canMessageImport('MsgLog.asc', 'Vector', candb, 'OutputFormat', 'timetable')
(my understanding is this code imports hex data for every row in the log file as cell array (as dec numbers) in the output)
Once the data is been loaded into workspace, I am converting the imported timetable data to table format using the below comand.
canMsgs = timetable2table(canMsgs).
The cell array loaded from CAN data is off different length (depending on the byte size (4,6,8)) and when I use the cell2mat function to split the cell array into individual columns I am getting the following error. I am looping through to do this with the below code
for i = 1:length(canMsgs.Data)
C(i,:) = cell2mat(canMsgs.Data(i));
end
Below is the error I am seeing
Subscripted assignment dimension mismatch.
Error in CAN_Test (line 23)
C(i,:) = cell2mat(canMsgs.Data(i));
I have attached a screenshot from matlab workspace showing the data size, below is the sample data. Individual line represents single cell array (can be seen in the attached image for reference)
[0,0,0,0,0,0,0,0]
[10,0,0,0,0,15,39,0]
[0,0,0,0,230,3,0,0]
[1,0,0,0,0]
[47,255,0,0,70,129,0]
[0,255,0,0,0,1]
[0,0,0,0,0,6,37,0]
[0,0,0,0,48,248,88,27]
[170,0,0,0,0,64,0,0]
Please help me to fix this issue.
Thanks in advance.

Réponse acceptée

Walter Roberson
Walter Roberson le 17 Jan 2020
Nmsg = length(canMsgs.Data);
msglen = cellfun(@length, canMsgs.Data);
maxlen = max(msglen);
C = nan(Nmsg, maxlen);
for i = 1:Nmsg
thismsg = canMsgs.Data{i};
C(i,1:length(thismsg)) = thismsg;
end
This creates C as a numeric matrix in which shorter messages are NaN padded to the length of the longest message. (You have to adopt some convention to mark shorter messages if the messages are not all the same length.)
  1 commentaire
Logesh Velusamy
Logesh Velusamy le 20 Jan 2020
This works perfect for me. Thanks a lot for your help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by