How can I loop through a column in a sub tables and convert non-cells to cells?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Matt J
2023-7-3
Sorry I cannot attach my data.
Meaning that it is proprietary? Then attach hypothetical data with the same relevant structure.
采纳的回答
Dyuman Joshi
2023-7-3
%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 个评论
Dyuman Joshi
2023-7-3
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
2023-7-17
编辑:Peter Perkins
2023-7-17
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.
更多回答(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!