How do I take data from one column of an Excel spreadsheet and store it as a transposed row in a new Excel Spreadsheet? How do I repeatedly take this column data from different Excel spreadsheets to be transposed as rows in the new Excel Spreadsheet?
3 views (last 30 days)
Show older comments
Zuha Yousuf
on 8 Aug 2019
Answered: Abhilash Padma
on 12 Aug 2019
So I have a lot of .xlsx files (I have attached one here). I need to take the data from column 7 (Heart Rate), from a specific number of rows (in THIS case, from row 1944 till row 2730) and input this data in the form of a row in a new Excel spreadsheet. I need to repeat this process for the rest of my xlsx files and store their 7th columns into the same new Excel spreadsheet. Is there a way to automate this process, keeping in mind that these different .xlsx files may have a different number of rows to be stored?
Below is the code I've used to import one of my .xlsx files into MATLAB. I have also attached the respective .xlsx file.
[~,NumDataS]=xlsread('Oxygen_6.10.19_Dex_0024_vitals.xlsx','Sheet2','F1944:G2730'); % Physiological Data
[~,TmatrixS] = xlsread('Oxygen_6.10.19_Dex_0024_vitals.xlsx','Sheet2','A1944:F2730'); % Time Data
HR = cellfun(@str2double, NumDataS(:,2)); % Retrieve From Cell Array Of Strings
Time1=cellfun(@str2double, TmatrixS(:,5));%Array of minutes to be converted to seconds
0 Comments
Accepted Answer
Abhilash Padma
on 12 Aug 2019
You can use the “writematrix” method to store a matrix in an excel spreadsheet. See the following code where this method is used.
[~,NumDataS]=xlsread('Oxygen_6.10.19_Dex_0024_vitals.xlsx','Sheet2','F1944:G2730'); % Physiological Data
[~,TmatrixS] = xlsread('Oxygen_6.10.19_Dex_0024_vitals.xlsx','Sheet2','A1944:F2730'); % Time Data
HR = cellfun(@str2double, NumDataS(:,2)); % Retrieve From Cell Array Of Strings
Time1=cellfun(@str2double, TmatrixS(:,5));%Array of minutes to be converted to seconds
writematrix(Time1','sample.xlsx','Range','A1');
If you want to store columns of different excel spreadsheets, consider a cell array which contains each cell as a filename. Then, put all the above statements in a loop. For example:
files={'Oxygen_6.10.19_Dex_0024_vitals.xlsx','example.xlsx',……………….};
row='A1';
for i=1:length(files)
[~,NumDataS]=xlsread(files{i},'Sheet2','F1944:G2730');
....
writematrix(Time1','sample.xlsx','Range',row);
row(2)=row(2)+1;
end
For more information, refer the following link: https://www.mathworks.com/help/matlab/ref/writematrix.html
0 Comments
More Answers (0)
See Also
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!