How to assign filtered data of tables to new tables and then store them into a data structure?

조회 수: 4 (최근 30일)
Hello to everyone I proceed to illustrate step by step my program and my issue The first program block import data from 4 .csv files and then store them within a data strcuture after performing some filtering conditions % STRUCTURE CREATION % Initialization of a struct array with "idx_L_point" and "data" fields no_L_points =2 Sol = repmat(struct('L_point','','all',[],'tang',[]),1,no_L_points);
% Import of results for i= 1:no_L_points % from 1 to number of L-points Sol(i).L_point = i;
Sol(i).all.general = readtable(['Solutions_General_L',num2str(i),'.csv'],'VariableNamingRule','preserve');
Sol(i).all.orbital = readtable(['Solutions_Orbital_L',num2str(i),'.csv'],'VariableNamingRule','preserve');
% Filter tangency cases and store in another filed of structure
Sol(i).tang.general = Sol(i).all.general(Sol(i).all.general.nInters==1,:);
Sol(i).tang.orbital = Sol(i).all.orbital(Sol(i).all.orbital.nInters==1,:);
end
In the second block I tried to manipulate these data for a further filtering
%%FILTERING THE MINIMUM VALUES
% Filter the tables by keeping only the ones that contains the minimum
% values.
% Candidate asteroids
Ast_candidates_L1 = unique(Sol(1).all.general.iAst)
Ast_candidates_L2 = unique(Sol(2).all.general.iAst)
for idx_cand_L1 = 1:numel(Ast_candidates_L1) cand_cond_L1 = Sol(1).all.general( Sol(1).all.general.iAst == Ast_candidates_L1(idx_cand_L1),: );
tab_L1_DV_min(idx_cand_L1,:) = cand_cond_L1(cand_cond_L1.("DV_inters(km/s)")== min(cand_cond_L1.("DV_inters(km/s)")),:);
tab_L1_TOF_min(idx_cand_L1,:) = cand_cond_L1(cand_cond_L1.("TOF_SC_Ly_inters(d)")== min(cand_cond_L1.("TOF_SC_Ly_inters(d)")),:);
tab_L1_dist_min(idx_cand_L1,:) = cand_cond_L1(cand_cond_L1.("dist_ast_SC_inters(km)")== min(cand_cond_L1.("dist_ast_SC_inters(km)")),:);
end
for idx_cand_L2 = 1:numel(Ast_candidates_L2) cand_cond_L2 = Sol(2).all.general( Sol(2).all.general.iAst == Ast_candidates_L2(idx_cand_L2),: ); idx_cand_L2 tab_L2_DV_min (idx_cand_L2,:) = cand_cond_L2(cand_cond_L2.("DV_inters(km/s)") == min(cand_cond_L2.("DV_inters(km/s)")) ,:); tab_L2_TOF_min(idx_cand_L2,:) = cand_cond_L2(cand_cond_L2.("TOF_SC_Ly_inters(d)")== min(cand_cond_L2.("TOF_SC_Ly_inters(d)")),:); tab_L2_dist_min(idx_cand_L2,:) = cand_cond_L2(cand_cond_L2.("dist_ast_SC_inters(km)")== min(cand_cond_L2.("dist_ast_SC_inters(km)")),:); end
I can't understand why there is the warning: "Error using () To assign to or create a variable in a table, the number of rows must match the height of the table." regarding the variable "tab_L2_DV_min". Infact i perform the same operations for all that variables but something went wrong with such a variable. 1st question) Can you clarify the reason of this warning and how to fix it ? (maybe show me a better way of coding ) In addition to this first problem, I was trying to create a new structure where to store the six filtered table I computed in the "for" cycles but I had problems with dynamical assignment of variables. The starting idea was to store the filtered tables in the field of structure represented by symbol "[]" in the attached image.
% DATA STRUCTURE FOR MINIMUM VALUES (work in progress)
Results_best = repmat(struct('L_point','','data_DV_min',[],'data_TOF_min',[],'data_dist__min',[]),1,no_L_points);
for i= 1:no_L_points % from 1 to number of L-points
Results_best(i).L_point = i;
Results_best(i).data_DV_min = ...
Results_best(i).data_data_TOF_min = ...
Results_best(i).data_dist__min = ...
end
2nd question) Can you show me a smarter way to code the filtering the minimum values and then to store them in the data structure for minimum values?

채택된 답변

Walter Roberson
Walter Roberson 2022년 9월 26일
tab_L1_DV_min(idx_cand_L1,:) = cand_cond_L1(cand_cond_L1.("DV_inters(km/s)")== min(cand_cond_L1.("DV_inters(km/s)")),:);
tab_L1_TOF_min(idx_cand_L1,:) = cand_cond_L1(cand_cond_L1.("TOF_SC_Ly_inters(d)")== min(cand_cond_L1.("TOF_SC_Ly_inters(d)")),:);
tab_L1_dist_min(idx_cand_L1,:) = cand_cond_L1(cand_cond_L1.("dist_ast_SC_inters(km)")== min(cand_cond_L1.("dist_ast_SC_inters(km)")),:);
What happens if there is more than one value in the table that is a match for the min() ? That would result in a right hand side table that has multiple rows, and you would be trying to assign it into a single row of the table.
If you are sure that there will be exactly one match, then take the second output of min() to get the index, and use the index to extract the row. This would require multiple statements, but note that those right hand sides do not change within the loop so they can be pre-computed.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Adaptive Filters에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by