how to write values in excel after fetching from other excel files

2 vues (au cours des 30 derniers jours)
Aiman Zara
Aiman Zara le 2 Mai 2023
Commenté : Aiman Zara le 2 Mai 2023
i used this code
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,'VariableNames', {'Disease', 'Cure'});
% Write the table to a new Excel file
new_file = 'newfile.csv';
writetable(disease_cure_table, new_file);
But I get this output, however I wanted it to be like, in a Disease Column there should be 2 rows, one for Fusarium wilt and 2nd for Bacterial Blight and in the Cure Column there should be cure feteched from both tables for Fusarium wilt in 1st Row of coloumn2 and Bacterial Blight in 2nd row of C2

Réponse acceptée

Walter Roberson
Walter Roberson le 2 Mai 2023
fusarium_cure1 = data1.Cure(fusarium_rows1);
fusarium_cure2 = data2.Cure(fusarium_rows2);
bacterial_cure1 = data1.Cure(bacterial_rows1);
bacterial_cure2 = data2.Cure(bacterial_rows2);
The variables such as fusarium_cure1 now hold content of the two excel tables.
% 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"]
all_cures and all_diseases are now constant strings. "fusarium_cure1" is the character vector 'f' 'u' 's' 'a' 'r' and so on, not the content of the variable named fusarium_cure1
disease_cure_table = table(all_diseases, all_cures,'VariableNames', {'Disease', 'Cure'});
You put the strings into the table, but you ignore the contents of the variables fusarium_cure1 and so on.
  3 commentaires
Walter Roberson
Walter Roberson le 2 Mai 2023
% Combine the cures into a single variable
all_cures = [fusarium_cure1; fusarium_cure2; bacterial_cure1; bacterial_cure2];
Aiman Zara
Aiman Zara le 2 Mai 2023
That is not working,
Error using table (line 231)
All table variables must have the same number of rows.
Error in Untitled (line 63)
disease_cure_table = table(all_diseases, all_cures,'VariableNames', {'Disease', 'Cure'});

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Food Sciences dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by