Cell2mat not working for unequal length cell data/array
9 ビュー (過去 30 日間)
古いコメントを表示
Logesh Velusamy
2020 年 1 月 17 日
コメント済み: Logesh Velusamy
2020 年 1 月 20 日
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.
0 件のコメント
採用された回答
Walter Roberson
2020 年 1 月 17 日
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.)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!