How to convert cell array into double in table
65 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi all, I've a table of the format
section D1 D2 L sectionID
____________________ ____ _____ _____ _________
1.00000000000000e+00 [ 5] [ 5] [ 6] [11]
2.00000000000000e+00 [12] [ 2] [ 34] [11]
3.00000000000000e+00 [12] [312] [323] [11]
4.00000000000000e+00 [ 3] [ 45] [ 23] [11]
5.00000000000000e+00 [ 4] [ 4] [ 23] [11]
6.00000000000000e+00 [ 2] [ 43] [ 23] [11]
7.00000000000000e+00 [ 3] [ 4] [ 23] [11]
8.00000000000000e+00 [ 4] [ 23] [ 32] [11]
Where Section is a double and rest D1,D2,L and section ID are cell arrays. I want to convert all these too into double arrays. I tried to do using cell2mat
temp_table.D1 = cell2mat(temp_table.D1);
temp_table.D2 = cell2mat(temp_table.D2);
temp_table.L = cell2mat(temp_table.L);
temp_table = cell2mat(temp_table.sectionID);
When I try to use the cell2mat in the above way, I get the following error
Field assignment to a non-structure array object.
Struct contents reference from a non-struct array object.
Error in sample_gui_ver2/tb9_Callback (line 1918)
temp_table.D1 = cell2mat(temp_table.D1);
Error while evaluating UIControl Callback
Can anyone help me to fix this issue. Thanks in advance.
0 comentarios
Respuestas (4)
Peter Perkins
el 31 de Mzo. de 2017
This whole thread begs the question of how the table got like that to begin with, which is worth you figuring out, probably. But I think what you're looking for is
>> t = table([1;2;3;4],{5;6;7;8},{9;10;11;12},[13;14;15;16])
t =
4×4 table
Var1 Var2 Var3 Var4
____ ____ ____ ____
1 [5] [ 9] 13
2 [6] [10] 14
3 [7] [11] 15
4 [8] [12] 16
>> for i = 1:width(t), if iscell(t.(i)), t.(i) = cell2mat(t.(i)); end, end
>> t
t =
4×4 table
Var1 Var2 Var3 Var4
____ ____ ____ ____
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
Alternatively, if all the vars in thetable are cells, use varfun:
>> t = table({1;2;3;4},{5;6;7;8},{9;10;11;12},{13;14;15;16})
t =
4×4 table
Var1 Var2 Var3 Var4
____ ____ ____ ____
[1] [5] [ 9] [13]
[2] [6] [10] [14]
[3] [7] [11] [15]
[4] [8] [12] [16]
>> t = varfun(@cell2mat,t)
t =
4×4 table
cell2mat_Var1 cell2mat_Var2 cell2mat_Var3 cell2mat_Var4
_____________ _____________ _____________ _____________
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
and then patch up the names. varfun is a bit less convenient when only some vars are cells, you'd end up having to interleave two pieces back together.
0 comentarios
Will Wilson
el 29 de Mzo. de 2017
Try using str2double to get the job done. Something like this:
temp_table.D1 = str2double (temp_table.D1);
1 comentario
Walter Roberson
el 29 de Mzo. de 2017
Notice that the error is
Field assignment to a non-structure array object.
The problem is the destination being assigned to, not the source. At some line above what you show, you have assigned a non-structure to temp_table, such as a numeric array, and then the assignment to temp_table.D1 is trying to treat the existing object as if it were a structure. Instead of just erasing all of the existing temp_table and creating a new structure, MATLAB complains that it is not already a structure.
The problem would not occur if temp_table were a structure (or table), or if temp_table did not exist at that point.
0 comentarios
Azzi Abdelmalek
el 4 de Abr. de 2016
Probably one of the columns is not a cell array. Check if all columns are cell array
0 comentarios
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!