Error using table (line 231) All table variables must have the same number of rows. Error in correct1 (line 35) disease_cure_table = table(all_​diseases,a​ll_Pests, all_cures,

3 views (last 30 days)
I have two excel files, need to find the commomn diseases in both and then write disease names and their cures in the same row in a new excel table, also find the coomon pests and write their names and cure but somehow I am getting the above error, please help me find the solution. The code I have worked on is % Load the two Excel files
% 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.Diseases_Pests, {'Fusarium wilt'});
fusarium_rows2 = ismember(data2.Diseases, {'Fusarium wilt'});
bacterial_rows1 = ismember(data1.Diseases_Pests, {'Bacterial Blight'});
bacterial_rows2 = ismember(data2.Diseases, {'Bacterial Blight'});
jassid_rows1 = ismember(data1.Diseases_Pests, {'Jassid'});
jassid_rows2 = ismember(data2.Pests, {'Jassid'});
thrips_rows1 = ismember(data1.Diseases_Pests, {'Thrips'});
thrips_rows2 = ismember(data2.Pests, {'Thrips'});
% Extract the cures for all 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);
jassid_cure1 = data1.Cure(jassid_rows1);
jassid_cure2 = data2.Cure(jassid_rows2);
thrips_cure1 = data1.Cure(thrips_rows1);
thrips_cure2 = data2.Cure(thrips_rows2);
% Combine the cures into a single variable
all_cures = {'fusarium_cure1'; 'fusarium_cure2'; 'bacterial_cure1'; 'bacterial_cure2'; 'jassid_cure1'; 'jassid_cure2'; 'thrips_cure1'; 'thrips_cure2' };
% Combine the diseases into a single variable
all_diseases = {'Fusarium wilt'; 'Fusarium wilt'; 'Bacterial Blight'; 'Bacterial Blight'; 'Jassid'; 'Thrips'};
all_Pests = {'Jassid'; 'Thrips'};
% Create a new table with the diseases and cures
disease_cure_table = table(all_diseases,all_Pests, all_cures, 'VariableNames', {'Diseases'; 'Cures'});
% Write the table to a new Excel file
new_file = 'good5.csv';
writetable(disease_cure_table, new_file);

Answers (1)

Jon
Jon on 12 May 2023
When in your DiseaseCure.csv file you only have two columns, Diseases-Pests, and Cure and then you read these into the table data1. But in you code you reference data1.Diseases and data1.Pests
The only variable in data1 will be Diseases_Pests (MATLAB converts the variable Diseases-Pests, to Diseases_Pests to make a valid name). So your code will get an error as soon as you reference either data1.Diseases or data1.Pests.
You seem to indicate a different error. Are you sure you've attached the correct code? If you do attach additional code, please use the "Code" button and cut and paste, so that it will be nicely formatted and easily copied.
  4 Comments
Jon
Jon on 12 May 2023
I'm sorry, but I don't have time at the moment to work through coding your whole problem, but can help with specific MATLAB problems.
I would suggest trying to restructure your Excel data, and thus the resulting tables you get from importing them, so that you just have one entry for each variable in a given row, no lists of entries. You can then use functions like join and innerjoin to select rows and make new tables based on common elements.

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!