How do I make my metaTable into a matrix instead of a 1x1 structure?
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I am trying to construct a table, or a matrix, and have written the following code for it. However, instead of it coming up as a table, it comes up as a 1x1 structure. I would like the table to have colums for all of the headers below that I have made into a metaTable (eg. proms_eff, avg_breath_width, avg_breath_prom, avg_instf_fq, and avg_acc). In the structure, each has the same length, so I'm not sure why it's not making the table, eg. matrix, that I want.
%loop through each LC
for lc = 1:length(P)
metaTable.proms_dff(lc) = proms_dff(lc);
metaTable.AVG_breath_width(lc) = mean(P{lc}(:,2));
metaTable.AVG_breath_prom(lc) = mean(P{lc}(:,3));
metaTable.AVG_inst_fq(lc) = mean((fs/50) ./ (diff(P{lc}(:,1))));
metaTable.AVG_acc(lc) = mean((diff(instantaneous_fq{lc}) / (fs/50)));
end
I could get rid of the entire final row of the column, because it comes up as 0 or NaN for these metrics, but when I type this instead of above, it still gives me the original number of rows.
%loop through each LC
for lc = 1:length(P)-1
One last question is if I had say 78 for the proms_dff, and then 77 for all of the rest, how would I make sure it would go into a table and just cut off the last one?
Thank you!
4 个评论
Dyuman Joshi
2023-12-21
"In the structure, each has the same length, so I'm not sure why it's not making the table"
The syntax for defining/creating a table is using table. The dot indexing is used to access the data inside table.
When the dot indexing is used to defining an array, the array created is a structure array.
If you want to get a table as the output, you can use struct2table as @KSSV has suggested, or preallocate the output variable as a table.
As for the last question, it's not clear to me what you are asking.
采纳的回答
Hassaan
2023-12-21
To create a table instead, the user should initialize a table before the loop and then add rows to it within the loop.
% Initialize the table with the correct variable names
metaTable = table();
% Loop through each LC
for lc = 1:length(P)
% Compute the values for each row
proms_dff_val = proms_dff{lc};
AVG_breath_width_val = mean(P{lc}(:,2));
AVG_breath_prom_val = mean(P{lc}(:,3));
AVG_inst_fq_val = mean((fs/50) ./ diff(P{lc}(:,1)));
AVG_acc_val = mean((diff(instantaneous_fq{lc})) / (fs/50));
% Create a temporary table for this iteration
tempTable = table(proms_dff_val, AVG_breath_width_val, AVG_breath_prom_val, AVG_inst_fq_val, AVG_acc_val);
% Append the temporary table to the main table
metaTable = [metaTable; tempTable];
end
In this code:
- The table function is used to create an empty table with no rows initially.
- Inside the loop, the user calculates the values for each column of the new row.
- A temporary table tempTable is created with these values for each iteration.
- This temporary table is then appended to the main table metaTable using vertical concatenation.
Regarding the final row having NaN or 0, MATLAB allows for table rows to have NaN values, and they will not affect the number of rows in the table. If a certain row has NaN or 0 and the user wishes to exclude it, they can do so after the loop by checking for NaN or 0 and removing those rows.
For the last question about different lengths for proms_dff compared to other metrics, the user should ensure that all columns are the same length before creating the table. If proms_dff has 78 elements and the others have 77, the user would have to handle this discrepancy either by removing the extra data point from proms_dff or by padding the other columns with NaN or another placeholder value to make them equal in length.
Lastly, to directly convert a structure to a table, one can use the struct2table function if the structure is properly formatted (e.g., each field contains a column of a table):
A basic example of how you might convert a structure array to a table in MATLAB.
Let's say we have a structure array with fields a, b, and c, each containing a numeric array. Here's how you would convert this structure array to a table:
% Create a structure array
S(1).a = 1; S(1).b = 2; S(1).c = 3;
S(2).a = 4; S(2).b = 5; S(2).c = 6;
S(3).a = 7; S(3).b = 8; S(3).c = 9;
% Convert the structure array to a table
T = struct2table(S);
% Display the table
disp(T);
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!