How do I make my metaTable into a matrix instead of a 1x1 structure?

3 vues (au cours des 30 derniers jours)
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 commentaires
Dyuman Joshi
Dyuman Joshi le 21 Déc 2023
"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.
Caroline Szujewski
Caroline Szujewski le 3 Jan 2024
Modifié(e) : Caroline Szujewski le 3 Jan 2024
I have a follow up question. I have gotten my code to work to make the table with the following code.
%% CALCULATE INSTANTANEOUS FREQUENCY
% instantaneous_fq = (fs/50) / (locs_sb(i) - locs_sb(i-1) )
%create a cell array that will store the instantaneous_fq at each event for
%each breath within each LC event
instantaneous_fq = cell(1,length(P));
metaTable = table();
%loop through each LC
for lc = 1:length(P)-1
instantaneous_fq{lc} = (fs/50) ./ (diff(P{lc}(:,1)));
% Compute the values for each row
metaTable.proms_dff(lc) = proms_dff(lc);
metaTable.widths_dff(lc) = widths_dff(lc);
metaTable.area_dff(lc) = ((proms_dff(lc)) * (widths_dff(lc)))/2;
metaTable.AVG_breath_width(lc) = mean(P{lc}(:,2));
metaTable.PEAK_breath_width(lc) = max(P{lc}(:,2));
metaTable.AVG_breath_prom(lc) = mean(P{lc}(:,3));
metaTable.PEAK_breath_prom(lc) = max(P{lc}(:,3));
metaTable.AVG_inst_fq(lc) = mean((fs/50) ./ (diff(P{lc}(:,1))));
metaTable.PEAK_inst_fq(lc) = max((fs/50) ./ (diff(P{lc}(:,1))));
metaTable.AVG_acc(lc) = mean((diff(instantaneous_fq{lc}) / (fs/50)));
metaTable.PEAK_acc(lc) = max((diff(instantaneous_fq{lc}) / (fs/50)));
end
I am trying to add in one final column of the metaTable where I find the row number of the PEAK_breath_prom(lc) in each cell array. I then want to find the value in that same row number in the first column. I want those values in the 1st column that correspond to the row with the peak value in the PEAK_breath_prom(lc) to make my final column in the cell array. What is the best way to go about indexing that row number and then finding the number in that same row in column 1, throughout the cell array to then populate the final metaTable columm?

Connectez-vous pour commenter.

Réponse acceptée

Hassaan
Hassaan le 21 Déc 2023
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);

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by