Renaming multiple excel sheets

68 vues (au cours des 30 derniers jours)
alexandra ligeti
alexandra ligeti le 18 Août 2022
Modifié(e) : dpb le 18 Août 2022
Hi there,
I am really struggling to get the sheet names in my code to be renamed.
I have different folders that hold different partcipants data, then within those folders I have three folders that hold different sensor data which contain a variety of activity data captured by these sensors.
I have managed to time synchronise the data for the three different sensors and I am then wanting to write this time synchronised data into an excel file. I have used the below code to write the new time synchronised data for the three trials carried out by the three differnet sensors into the excel file and into the new folder ''TimeSyncData''.
%% Create a folder where the data will be saved
OutputFolderName = 'TimeSyncData';
if ~exist(OutputFolderName, 'dir')
mkdir(OutputFolderName);
end
cd(OutputFolderName)
%creating an excel sheet with the new time synchronised data contained,
%different sheets are the different trials
for i = 1:sz
T = struct2table(sync(i));
writetable(T,strcat(activity,'.xls'),'Sheet',i);
I am wondering how I would go about renaming the three new sheets that contain my three different trials held within them. I have tried using this:
%%Added 17/098/2022
% excel = actxserver('Excel.Application'); % # open Activex server
% filepath = pwd;
% excelfilename = activity,'.xls';
% excelWorkBook = excel.Workbooks.Open(fullfile(filepath,excelfilename));
% excelWorkBook.Worksheets.Item(1).Name = 'Trial 1';
% excelWorkBook.Worksheets.Item(2).Name = 'Trial 2';
% excelWorkBook.Worksheets.Item(3).Name = 'Trial 3';
% excelWorkBook.Save % # save to the same file
% excelWorkBook.Close(false)
% excel.Quit
However, it does not work, it only changes the name for one sheet and then it makes the excel file open up in the background without me being able to visibly see it. Any advice or help would be greatly appreciated. I can also attached the full working code if that would be of any use?
Thank you so much in advace,
Lexi

Réponse acceptée

dpb
dpb le 18 Août 2022
Just name them as you want them when created them instead...
%% Create a folder where the data will be saved
OutputFolderName = 'TimeSyncData';
if ~exist(OutputFolderName, 'dir')
mkdir(OutputFolderName);
end
%creating an excel sheet with the new time synchronised data contained,
%different sheets are the different trials
for i = 1:sz
writetable(struct2table(sync(i)),fullfile(OutputFolderName,[activity '.xlsx']), ...
'Sheet',"Trial "+i);
  4 commentaires
alexandra ligeti
alexandra ligeti le 18 Août 2022
Thank you so much! I have managed to get the file naming issue sorted, the program now navigates between the different folders correctly. The renaming of the sheets works now too!
Thanks again for your help.
dpb
dpb le 18 Août 2022
Modifié(e) : dpb le 18 Août 2022
I knew you could... :)
BTW, I've not tried the specific exercise you tried so not positive and didn't take the time to go debug, but I expect what happens in the rename exercise is that the Items collection gets shuffled when the new name is written and your code doesn't update to see which sheet is which in the collection order.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by