How to display matrices in cell array in a table?
Show older comments
I have a cell array, and each element of the cell array is a matrix. But when I use cell2table to show the array, the table only shows that it is a 4*1 matrix instead of showing the actual value of the matrix. Is there a way to use cell2table to display the matrix values?
Here is my code, the cell array with matrix is named "x". Currently, I have to use celldisp, but it is not a presentable format.
clear
close all
clc
Q1 = [1 0 0 0; 0 1 0 0; 0 0 0 0; 0 0 0 1];
Q2 = [2 0 0 0; 0 0 0 0; 0 0 1 0; 0 0 0 1];
Q3 = [2 0 0 0; 0 0 0 0; 0 0 1 0; 0 0 0 2];
Q4 = [2 0 0 0; 0 0 0 0; 0 0 2 0; 0 0 0 2];
Q5 = [2 0 0 0; 0 0 0 0; 0 0 2 0; 0 0 0 1];
%{
[u,x] = longdyne(Q1);
cell2table(u,"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
%cell2table(x,"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
celldisp(x)
%}
%
[u,x] = longdyne(Q2);
Q2x = cell2table(u,"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
%cell2table(x,"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
celldisp(x)
%}
%
[u,x] = longdyne(Q3);
Q3x = cell2table(u,"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
%cell2table(x,"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
celldisp(x)
%}
%
[u,x] = longdyne(Q4);
Q4x = cell2table(u,"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
%cell2table(x,"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
celldisp(x)
%}
%
[u,x] = longdyne(Q5);
Q5x = cell2table(u,"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
%cell2table(x,"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
celldisp(x)
%}
function [u,x] = longdyne(QN)
A = [0.994 0.026 0 -32.2; -0.094 0.376 820 0; 0 -0.002 0.332 0; 0 0 1 1];
B = [0;-32.7;-2.08;0];
x = cell(1,11);
x{1,1} = [0;10;0;0];
u = cell(1,10);
R = 0;
P = cell(1,10);
P{1,10} = QN;
F = cell(1,10);
F{1,10} = -1*((R+B.'*P{1,10}*B)^(-1))*B.'*P{1,10}*A;
for i = 9:-1:1
P{1,i}=A.'*P{1,i+1}*A-A.'*P{1,i+1}*B*((R+B.'*P{1,10}*B)^(-1))*B.'*P{1,10}*A+QN;
F{1,i} = -1*((R+B.'*P{1,i+1}*B)^(-1))*B.'*P{1,i+1}*A;
end
%celldisp(F)
for i = 1:10
u{1,i}=F{1,i}*x{1,i};
x{1,i+1}=A*x{1,i}+B*u{1,i};
end
end
Answers (2)
No, there is not. table() objects are not designed for display purposes. In every case in which an entry is multiple lines, table() will only display sizes and datatype.
T = table({1}, {(1:2).'}, {1:2}, {(1:3).'}, {1:3}, {1:20})
However, row vectors sometimes have their values displayed.
you can use array2table & cell2tmat together to display cell array content present in the table as shown below
clear
close all
clc
Q1 = [1 0 0 0; 0 1 0 0; 0 0 0 0; 0 0 0 1];
Q2 = [2 0 0 0; 0 0 0 0; 0 0 1 0; 0 0 0 1];
Q3 = [2 0 0 0; 0 0 0 0; 0 0 1 0; 0 0 0 2];
Q4 = [2 0 0 0; 0 0 0 0; 0 0 2 0; 0 0 0 2];
Q5 = [2 0 0 0; 0 0 0 0; 0 0 2 0; 0 0 0 1];
[u,x] = longdyne(Q1);
Q1x = array2table(cell2mat(u),"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
array2table(cell2mat(x),"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
[u,x] = longdyne(Q2);
Q2x = array2table(cell2mat(u),"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
array2table(cell2mat(x),"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
[u,x] = longdyne(Q3);
Q3x = array2table(cell2mat(u),"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
array2table(cell2mat(x),"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
[u,x] = longdyne(Q4);
Q4x = array2table(cell2mat(u),"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
array2table(cell2mat(x),"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
[u,x] = longdyne(Q5);
Q5x = array2table(cell2mat(u),"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
array2table(cell2mat(x),"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
%}
function [u,x] = longdyne(QN)
A = [0.994 0.026 0 -32.2; -0.094 0.376 820 0; 0 -0.002 0.332 0; 0 0 1 1];
B = [0;-32.7;-2.08;0];
x = cell(1,11);
x{1,1} = [0;10;0;0];
u = cell(1,10);
R = 0;
P = cell(1,10);
P{1,10} = QN;
F = cell(1,10);
F{1,10} = -1*((R+B.'*P{1,10}*B)^(-1))*B.'*P{1,10}*A;
for i = 9:-1:1
P{1,i}=A.'*P{1,i+1}*A-A.'*P{1,i+1}*B*((R+B.'*P{1,10}*B)^(-1))*B.'*P{1,10}*A+QN;
F{1,i} = -1*((R+B.'*P{1,i+1}*B)^(-1))*B.'*P{1,i+1}*A;
end
%celldisp(F)
for i = 1:10
u{1,i}=F{1,i}*x{1,i};
x{1,i+1}=A*x{1,i}+B*u{1,i};
end
end
hope this helps
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!