how to fetch data from excel and store it in another excel file

I have a data in excel table, I want to fetch some data from that table and then store it in anaother table. How to do that please guide me

1 Comment

No big deal, but you've got to start...the basics with plenty of examples for specifically the problem you're asking about are at the <following top level link>; the most pertinent specific functions will undoubtedly be readtable and writetable

Sign in to comment.

Answers (2)

% Read data from input Excel file
[data, headers] = xlsread('input_file.xlsx');
Error using xlsread
Unable to open file 'input_file.xlsx'.
File '/users/mss.system.qWEE3v/input_file.xlsx' not found.
% Manipulate the data as required
new_data = data + 1;
% Write the manipulated data to output Excel file
xlswrite('output_file.xlsx', [headers; num2cell(new_data)]);
Hi Aiman,
I understand that you are trying to fetch the data from an excel file and then store it into another excel file.
To fetch the data from the input Excel file into a table, use the “readtable” function.
  • To read the data from a sheet named “sheet1” in an Excel file named “in.xlsx”, use the following command.
inTab=readtable('in.xlsx','Sheet','Sheet1');
Here, “inTab” contains the data from the Sheet in the form of a MATLAB table.
For more information regarding “readtable” function, refer the following link:
To write the data to another Excel file, use the “writetable” function.
  • To write the data to a sheet named “sheet1” in a new excel file named “out.xlsx”, use the following command.
writetable(inTab,'out.xlsx','Sheet','Sheet1');
For more information regarding “writetable” function, refer the following link:
I hope this answer will be helpful.

6 Comments

If I have to write the diseases of Cauliflower from both the tables then how will I write it in new excel file. That should display Cauliflower in one column and in second coloumn there should be diseases from both the tables. Please help @Nithin Kumar, files are attached
What have you tried so far? Where did you run into problems other than waiting for somebody else to do your work for you...
@dpb Ofcourse i did, In a newexcel file I want to write the diseases that are common in both files in one coloumn and in second coloumn should be the cure from both the files of those respected diseases I tried using the below code for two diseases but it gave me an error, however i made the rows equal too. Please help me find the solution. (Files are attached)
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);
The code i used is
% 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'});
% Combine the cures into a single variable
all_cures = ["fusarium_cure1", "fusarium_cure2", "bacterial_cure1", "bacterial_cure2"];
That is a row vector of string() objects
% Combine the diseases into a single variable
all_diseases = ["Fusarium wilt"; "Fusarium wilt"; "Bacterial Blight"; "Bacterial Blight"];
That is a column vector of string() objects.
% Create a new table with the diseases and cures
disease_cure_table = table(all_diseases, all_cures);
The number of rows in the row vector all_cures (that is, 1 row) is not the same as the number of rows in the column vector all_diseases (that is, 4 rows)
table() does not automatically reshape variables into columns.
@Walter Roberson sorry, I don't get your answer? you could not find any solution ?
all_cures = ["fusarium_cure1", "fusarium_cure2", "bacterial_cure1", "bacterial_cure2"]
all_cures = 1×4 string array
"fusarium_cure1" "fusarium_cure2" "bacterial_cure1" "bacterial_cure2"
all_diseases = ["Fusarium wilt"; "Fusarium wilt"; "Bacterial Blight"; "Bacterial Blight"]
all_diseases = 4×1 string array
"Fusarium wilt" "Fusarium wilt" "Bacterial Blight" "Bacterial Blight"
Notice the first one, the one you used commas for, is one single row with four columns
Notice the second one, the one you used semi-colons for, is one single column with four rows.
table() requires that all the variables have the same number of rows .
So be consistent: either use commans for both arrays, or use semi-colons for both arrays.

Sign in to comment.

Products

Release

R2019a

Asked:

on 25 Apr 2023

Commented:

on 1 May 2023

Community Treasure Hunt

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

Start Hunting!