Conditional data extraction from csv file
조회 수: 2 (최근 30일)
이전 댓글 표시
I wish to extract the data from the file 'ddata' into separate excel files containing only the variable columns of
v7,v8,v9 & v10 with respect to the varying values of v3,v4,v5 & v6. The values of v3 varies from 1 to 5, v4 varies from 1 to 2,
v5 varies from 1 to 3, and v6 varies from 1 to 8. For example, the condition when v3=1,v4=1,v5=1 & v6=1 will create the first
excel file. Please help.
댓글 수: 5
dpb
2022년 7월 24일
편집: dpb
2022년 7월 24일
I said it was "air code" -- the format string isn't enclosed in brackets to pull it all together into one string...
>> i=1;sprintf(['Data' repmat('_%02d',1,4) '.xlsx'],id1(i),id2(i),id3(i),id4(i))
ans =
'Data_01_01_01_01.xlsx'
>>
The Q? still is, why do you actually have to have files instead of just processing the groups?
답변 (1개)
Siraj
2023년 9월 5일
Hi! It is my understanding that you want to find all the unique combinations of values of column “v3”, “v4”, “v5” & “v6”, and now corresponding to these unique values you want to extract values of other columns and write those extracted values in a different excel file.
Begin by reading the data into a table using the "readtable" function. You can find more information about this function in the following link:
Next extract “v3”, “v4”, “v5” & “v6” as an array. Use the "unique" function to find all the unique combinations of these values. Refer to the following link for more details on how to use this function: https://www.mathworks.com/help/matlab/ref/double.unique.html
Iterate through each unique combination and extract the corresponding values from the other columns. Store these values in a temporary table. Write the temporary table to a separate Excel file. Refer to the link below to learn how to write a table to a file from MATLAB.
Refer to the example code below for better understanding.
% Create a sample table
T = table([1; 2; 3; 1; 2], [4; 5; 6; 4; 5], [7; 8; 9; 7; 8], [1;2;1;2;3],[1;2;1;2;3], 'VariableNames', {'Column1', 'Column2', 'Column3','Column4','Column5'});
disp(T);
% Extract the first three columns as an array
columns123 = table2array(T(:, 1:3));
% Find the unique combinations
uniqueCombinations = unique(columns123, 'rows');
for i = 1 : size(uniqueCombinations,1) %looping thorough the unique combinations
temp_T = T(T.Column1 == uniqueCombinations(i,1) & T.Column2 == uniqueCombinations(i,2) & T.Column3 == uniqueCombinations(i,3), ["Column4", "Column5"]);
disp(temp_T);
% saving the extracted values into an excel file
filename = "table_" + num2str(i)+".xlsx"; %generating the filename
writetable(temp_T,filename)
end
To learn more about “tables” in MATLAB refer to the link below.
Hope this helps.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!