Renaming multiple excel sheets

조회 수: 61 (최근 30일)
alexandra ligeti
alexandra ligeti 2022년 8월 18일
편집: dpb 2022년 8월 18일
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

채택된 답변

dpb
dpb 2022년 8월 18일
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
alexandra ligeti
alexandra ligeti 2022년 8월 18일
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 2022년 8월 18일
편집: dpb 2022년 8월 18일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by