Creating a Table from a table of cells with different lengths
23 views (last 30 days)
Show older comments
I was creating a program that is pulling labels and bus data stored in .txt files. One of the goals of the program was to output which lables are in each flies. Right now I'm stuck at creating table from a table that contains tables (I'm not sure what this is called in matlab, nested tables?). Here's my code now:
files=dir('*.txt');
numfiles=length(files);
for i=1:numfiles
arincData{i}=readtable(strcat(files(i).folder,'\',files(i).name), 'Format','%s%s%s%s%s%s');
G=groupcounts(arincData{i},'Label');
temp{i}=G{:,1};
%labels(:,i)=cell2table(temp{i}); %doenst work because temp has different lengths
end
The table "arincData" is a 1x8 cell and each cell is a table (what is the proper name for this?).
1593270x6 table 1593279x6 table 1197600x6 table 15794215x6 table 15794195x6 table 15794200x6 table 15793978x6 table []
"temp" is the following 1x8 cell
17x1 cell 17x1 cell 14x1 cell 72x1 cell 71x1 cell 71x1 cell 71x1 cell []
So how do I convert "temp" to a table? I guess it would have to be a 72x8? Is there any way to do it in the for loop above?
Also if I want to change the name of column, lets say the first column of arincData{1}, how do I do that? Matlab doesnt seem to like arincData{1}.Properties.VariableNames...
1 Comment
Matt Butts
on 4 May 2022
The table "arincData" is a 1x8 cell and each cell is a table (what is the proper name for this?).
This is simply a cell array. Within a cell array, each cell can have different data types. Your cell array happens to have tables in each cell.
On turning "temp" into a table, what is the ultimate desire for how the data is structured?
Based on your original pronblem statement and what you have shown so far, there may be some flaws in yuor logic. If you are really just wanting to know which labels are in each file, wouldn't something like:
unique(arincData{:,'Label'})
work better?
Answers (1)
Vatsal
on 17 Nov 2023
Hi,
In MATLAB, a cell array that contains tables is often referred to as a "cell array of tables". Each cell in the array can contain a table of different dimensions. Regarding your issue with creating a table from tables of different lengths, MATLAB requires that all variables (columns) in a table have the same number of rows. Therefore, you cannot directly concatenate tables of different lengths into one table. However, you can handle this by padding shorter tables with 'NaN' or some other placeholder value. This way, all tables will have the same length and can be concatenated into one table.
Regarding changing the column names, you can use the “Properties.VariableNames” property of the table. To change the name of the first column of “arincData{1}”, you can do the following:
arincData{1}.Properties.VariableNames{1} = 'NewColumnName';
In this example, “arincData{1}” represents the first table in the cell array, and “Properties.VariableNames{1}” allows you to access and modify the name of the first column.
I hope this explanation helps!
0 Comments
See Also
Categories
Find more on Logical 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!