How to copy specific column data from all excel sheets and paste it into a new excel sheet by concatenating obtained column data from each sheet below each other??
Show older comments
Hi, I have an excel workbook with name 'Bilal.xlsx'. This excel workbook consists of 2 sheets which contains data with 2 columns and 4 rows. Now i need to copy data from sheet 1 such as only from 2nd column from sheet 1 and paste it into new sheet let say 'Sheet A'. Similarly, then i need to copy data from 2nd column of sheet 2 and paste it into the same sheet 'Sheet A' but now the data should be pasted such as:
Sheet1.xlsx =
Name Age
Tom 20
Dick 15
Harry 25
Sheet2.xlsx =
Name Age
Tom 4
Dick 5
Harry 8
After Pasting 'Sheet A' must be look like:
Age
20
15
25
4
5
8
Actually, I have huge excel data and i want to do it for several sheets can anybody tell me how to write a code in MATLAB for such kind of problem. Thanks in advance
9 Comments
Bob Thompson
on 16 Jan 2019
You can use xlsread and xlswrite to do this, but if you don't already know how to do this in Matlab then you might be better off learning to do it with an excel macro, because the macro will run more quickly.
Ahmad Bilal
on 16 Jan 2019
Ahmad Bilal
on 16 Jan 2019
Bob Thompson
on 16 Jan 2019
Why are you looking for a Matlab code, specifically, to do this? Do you have a specific reason for not using Excel?
Are you trying to learn how to do this on your own, or just trying to get a functioning code that you can use without hassle?
Ahmad Bilal
on 16 Jan 2019
Bob Thompson
on 16 Jan 2019
Ok. The best way to learn to do this in Matlab is to review the documentation.
If I were to do this process I would read in the Excel data using the xlsread() command. Then I would manipulate the data within Matlab until it was formatted the way I want it (using indexing and arrays), and then I would print the data back out using the xlswrite() command.
Ahmad Bilal
on 16 Jan 2019
Bob Thompson
on 16 Jan 2019
Edited: Bob Thompson
on 16 Jan 2019
If you are going to use xlsread for multiple sheets then you will need to loop the command to call each sheet. xlsread can only call a single sheet at a time. I would recommend saving the data into a three dimensional array to help organize it for manipulation later.
for i = 1:ns; % ns is number of sheets
data(:,:,i) = xlsread(file,i,range); % Define your range as needed
end
If you need to print the data to multiple sheets as well, then the process is simply reversed.
Note that each time you call xlsread or xlswrite you are opening and closing the excel file. This can be a relatively slow process, so it is generally good practice to call it as few times as possible. For that reason I suggest you combine all of the data you want to print onto each sheet before you print it, that way you only need to print the data to each sheet once.
I have not personally used it, but I have heard that readtable() and writetable() can be used as an alternative to xlsread() and xlswrite(). I don't know that they are any quicker, but is generally used to help organize and present data in a more user friendly manner.
Ahmad Bilal
on 17 Jan 2019
Answers (0)
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!