How can I loop through a column in a sub tables and convert non-cells to cells?

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:

6 comentarios

Please attach your data using the paperclip button and show what you have attempted yet.
I have attempted to solve my issue with the for loop below:
for idx = 1:numSubTable
if Book1.SubTable(:,13) == 0
Book1.SubTable(:,13) = False
elseif Book1.SubTable(:,13) == 1
Book1.SubTable(:,13) = True
end
end
Dyuman Joshi
Dyuman Joshi el 3 de Jul. de 2023
Editada: Dyuman Joshi el 3 de Jul. de 2023
You did not attach your data. Please attach your data.
And note that True/False are equivalent to 1/0, so the assignments in if-else condition block is redundant.
Or do you want to use the char arrays 'true' and 'false' instead of logical values?
Sorry I cannot attach my data.
I'd like to either turn the logical values to cells or char arrays 'true' or 'false'.
Sorry I cannot attach my data.
Meaning that it is proprietary? Then attach hypothetical data with the same relevant structure.
okay, I've attached hypothetical data in the question. Also here in the comment.

Iniciar sesión para comentar.

 Respuesta aceptada

%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)
T = 9×3 table
Charge Orbital Atoms ______ _______ _____ Type1 S1 0 Typ2 S2 0 Type3 S3 0 Type1 S1 0 Typ2 S2 0 Typ2 S2 1 Type3 S3 1 Typ2 S2 1 Type1 S1 1
%values to replace
str = {"false"; "true"};
%Using indexing to update the table
T.Atoms = vertcat(str{T.Atoms+1})
T = 9×3 table
Charge Orbital Atoms ______ _______ _______ Type1 S1 "false" Typ2 S2 "false" Type3 S3 "false" Type1 S1 "false" Typ2 S2 "false" Typ2 S2 "true" Type3 S3 "true" Typ2 S2 "true" Type1 S1 "true"

3 comentarios

When trying to write the updated table back to the subtable, I get the error below:
Right hand side of an assignment into a table must be another table or a cell array.
Here is the code I wrote to loop through each SubTable:
for idx = 1:num
AtomsTable = Book1.SubTable{idx,1}(:,13);
str = {"False" ; "True"};
AtomsTable.Atoms = vertcat(str{AtomsTable.Atoms+1});
Book1.SubTable{idx,1}(:,13) = AtomsTable.Atoms
end
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.
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 3 de Jul. de 2023

Editada:

el 17 de Jul. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by