Changing the name of the sheet in excel during a loop for

17 views (last 30 days)
hello!
I have a bunch of xlsx files with this names:
20201214_PS0.xlsx
20201214_PS1.xlsx
20201214_PS2.xlsx
20201214_PS3.xlsx
...
I need to change the sheet of each xlsx file with the last part of the name (PS0, PS1, PS2).
I'm trying with xlsheets from Fahad Al Mahmood (2020) https://www.mathworks.com/matlabcentral/fileexchange/4474-xlsheets, MATLAB Central File Exchange. Retrieved December 27, 2020, but I'm not finding the way.
I can locate the part of the name that I need using strfind but I'm not able to put each name in the corresponding sheet and I'm not sure if it is possible.
It would be wonderful if someone can guide me about this question.
If you think that attaching the script may help, I'll do it.
Thank you very much in advance
Merry christmas

Accepted Answer

dpb
dpb on 27 Dec 2020
Edited: dpb on 27 Dec 2020
Each workbook just has one sheet that is to be named with the trailing sequence of the filename?
That should be pretty simple, if so.
d=dir('20201214*.xlsx'); % get the list of files
for i=1:numel(d)
xlsheets(extractBetween(d(i).name,'_','.'),d(i).name);
end
If you're trying to put all the sheets into one workbook, this function won't do that, it just names (and adds new ones if needed) the sheets in the workbook; it doesn't write any content to the sheet.
To do that, you'd read each of the files in the above loop and write to a new file with the sheet name--but wouldn't need this function, just use read/writetable or similar functions and specify the sheet name when writing it to begin with.
  9 Comments
dpb
dpb on 28 Dec 2020
Edited: dpb on 28 Dec 2020
"I replaced fnxxxx for the file name I was using (archivo_salida4) instead of d(i).name..."
Yeah, my bad. I had a variable in my workspace fn I didn't want to override and wasn't too convenient to build something that dir would return that matched the pattern so I just made up a temporary string to ensure I remembered correctly that extractBetween would return a cellstr which the FEX submission wanted.
The function could have been written to have been more tolerant of other forms of inputs, but was written before the new string class was introduced.
Anyways, I just pasted the line in and forgot to make the edit to make it general...that's what old age does to one...short term memory goes away.
If you want to get somewhat more cryptic,
d=dir('20201214*.xlsx');
arrayfun(@(i) xlsheets(extractBetween(d(i).name,'_','.'),d(i).name),1:numel(d));
or
d=dir('20201214*.xlsx');
cellfun(@(f) xlsheets(extractBetween(f,'_','.'),f),{d.name});
Alberto Martínez
Alberto Martínez on 3 Jan 2021
thank you very much!
I strongly appreciate your support.
Now that this part is fixed, I'm dealing with the plots. I think that I'm improving each day and I hope I can advance in my script by myself.
Anyway, thank you again to all of you!
Happy new year!
Cheers

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 27 Dec 2020
Edited: Image Analyst on 27 Dec 2020
In the loop:
% Get name without the extension
[~, baseFileNameNoExt, ext] = fileparts(fileName)
% Get last 3 characters
sheetName = baseFileNameNoExt(end - 2 : end);
xlswrite(xlFullFileName, Data, sheetName, 'A1');
OR
% Get name without the extension
[~, baseFileNameNoExt, ext] = fileparts(fileName)
% Take what's after the last underline.
underLines = find(baseFileNameNoExt == '_');
sheetName = baseFileNameNoExt(underLines(end) + 1 : end);
xlswrite(xlFullFileName, Data, sheetName, 'A1');
  2 Comments
Image Analyst
Image Analyst on 27 Dec 2020
OK there are several ways to do this. Do you know if there is an ActiveX function to rename a sheet? If so use it. You can find out what it is by recording a macro and looking at the macro's code. Alternatively you can use my attached class to duplicate a sheet with a new name, and then delete the old one.
Alberto Martínez
Alberto Martínez on 28 Dec 2020
thank you very much! I finally achieved it using xlsheets with the guides provided by dpb.
Best wishes!

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!