How to write two columns data in new excel, from the already available two excel files

7 次查看(过去 30 天)
i want to write the common diseases and their cure from the two excel files(attached). The new table should contain two columns one for Diseases and the other for Cure. The code I used is giving me unequal no of rows error, could not find a way to resolve this. Please help get through this or find me a second solution.
% Load the two Excel files
file1 = 'DiseaseCure.csv';
file2 = 'ExcelStages.csv';
data1 = readtable(file1);
data2 = readtable(file2);
% Find the rows in both files that have "Fusarium wilt" and "Bacterial Blight"
fusarium_rows1 = ismember(data1.Disease, {'Fusarium wilt'});
fusarium_rows2 = ismember(data2.Diseases, {'Fusarium wilt'});
bacterial_rows1 = ismember(data1.Disease, {'Bacterial Blight'});
bacterial_rows2 = ismember(data2.Diseases, {'Bacterial Blight'});
% Extract the cures for both diseases from both files
fusarium_cure1 = data1.Cure(fusarium_rows1);
fusarium_cure2 = data2.Cure(fusarium_rows2);
bacterial_cure1 = data1.Cure(bacterial_rows1);
bacterial_cure2 = data2.Cure(bacterial_rows2);
% Combine the cures into a single variable
all_cures = ["fusarium_cure1", "fusarium_cure2", "bacterial_cure1", "bacterial_cure2"];
% Combine the diseases into a single variable
all_diseases = ["Fusarium wilt"; "Fusarium wilt"; "Bacterial Blight"; "Bacterial Blight"];
% Create a new table with the diseases and cures
disease_cure_table = table(all_diseases, all_cures);
% Write the table to a new Excel file
new_file = 'newfile.csv';
writetable(disease_cure_table, new_file,'VariableNames', {'Disease', 'Cure'});

回答(2 个)

Walter Roberson
Walter Roberson 2023-5-1
A = [1, 2, 3, 4]
A = 1×4
1 2 3 4
B = [5; 6; 7; 8]
B = 4×1
5 6 7 8
table(A, B)
Error using table
All table variables must have the same number of rows.
  1 个评论
Walter Roberson
Walter Roberson 2023-5-1
A = [1, 2, 3, 4]
A = 1×4
1 2 3 4
B = [5, 6, 7, 8]
B = 1×4
5 6 7 8
table(A, B)
ans = 1×2 table
A B ________________ ________________ 1 2 3 4 5 6 7 8
A = [1; 2; 3; 4]
A = 4×1
1 2 3 4
B = [5; 6; 7; 8]
B = 4×1
5 6 7 8
table(A, B)
ans = 4×2 table
A B _ _ 1 5 2 6 3 7 4 8

请先登录,再进行评论。


Aiman Zara
Aiman Zara 2023-5-2
@Walter Roberson From the above code I changed the following lines
all_cures = {fusarium_cure1; fusarium_cure2; bacterial_cure1; bacterial_cure2};
% Combine the diseases into a single variable
all_diseases = {'Fusarium wilt'; 'Fusarium wilt'; 'Bacterial Blight'; 'Bacterial Blight'};
And I got the above output, this is fine but I wanted it to be like Fusarium wilt under disease Column and cure from both tables under a cure Column, in front of the fusarium wilt row and same for Bacterial Blight. Attaching the pic for reference.
Please help me to modify it

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by