How can I loop through a column in a sub tables and convert non-cells to cells?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Rookie Programmer
el 3 de Jul. de 2023
Editada: Peter Perkins
el 17 de Jul. de 2023
I have a 33x5 table (Named: Book1), each cell in the 4th column has sub table (Named: SubTable) of size Nx13.
When trying to concatinate all sub tables, I get the error below: "Cannot concatenate the table variable "Atoms" because it is a cell in one table and non-cell in another."
"Atoms" is the 13th column of the sub tables.
How can I loop through and convert all non-cells to cells in all the subtables (specifically from the 13th column)?
NOTE: In the sub tables, the 13th column "Atoms":
-The non-cells use logical have the value of 1 or 0.
-The cells have the value of 'true' or 'false'
Example of the SubTable:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1425208/image.png)
6 comentarios
Matt J
el 3 de Jul. de 2023
Sorry I cannot attach my data.
Meaning that it is proprietary? Then attach hypothetical data with the same relevant structure.
Respuesta aceptada
Dyuman Joshi
el 3 de Jul. de 2023
%Data to create the table posted
arr1 = categorical(["Type1" "Typ2" "Type3"]);
arr2 = categorical(["S1" "S2" "S3"]);
Charge = arr1([1 2 3 1 2 2 3 2 1])';
Orbital = arr2([1 2 3 1 2 2 3 2 1])';
Atoms = [0 0 0 0 0 1 1 1 1]';
%Table posted above
T = table(Charge,Orbital,Atoms)
%values to replace
str = {"false"; "true"};
%Using indexing to update the table
T.Atoms = vertcat(str{T.Atoms+1})
3 comentarios
Dyuman Joshi
el 3 de Jul. de 2023
This is why I requested the data from you, to know the format in which data is stored in.
Since I don't know how the data is stored in variable Book1, it's difficult to comment/suggest anything.
Please provide an example of how data is stored in the variable Book1, preferabbly in a .mat file, and not a picture.
Peter Perkins
el 17 de Jul. de 2023
Editada: Peter Perkins
el 17 de Jul. de 2023
I would think the thing to do is convert the text to logical, not the other way around.
If you are saying that your "outer table" contains a variable that is a cell array each cell containing an Nx13 table, and each of those "inner tables" has a var named Atoms, but some of them have Atoms as logical and some as string, you can fix that with something like
t.Atoms = cellfun(@myfun,Book1.SubTables)
where myfun is something like
function t = myfun(t)
if isstring(t.Atoms)
t.Atoms = t.Atoms == "true";
end
And then I gues you are doing something like
vertcat(Book1.Subtables{:})
That's as much sense as I can make of this.
Más respuestas (0)
Ver también
Categorías
Más información sobre Cell Arrays 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!