MATLAB: How to save matlab file according to excel sheet name

convertexcelmatMATLABsheetstrcatworkbookworksheetxlsreadxlsx

I am trying to convert an .xlsx file to .mat and save the .mat file by the sheet I refer to while importing. I am able to save the .mat file by the .xlsx workbook name automatically by defining the workbook name as a variable and using the srtcat() function. However, when I define the worksheet name as a variable and use the same srtcat() function (with worksheet name variable) the script does not save.
For the file recognition and saving portion of my script I currently have:
file_list={'filename','filename'};
sheet_list= {'sheet1','sheet2'};
for q = 1:length(file_list)
s = sheet_list{q};
c = file_list{q};
e = '.xlsx';
xlsx_file = xlsread(strcat(c,e),s);
% ....imports data....
save (strcat(c))
When I substitute 's' in for 'c', the code does not save a file. Conversely, when I have save(strcat(c)), it works fine.
Is there another way to save the .mat file by the excel worksheet name?
Thanks.

Best Answer

Try this:
file_list={'filename1', 'filename2'};
sheet_list= {'sheet1', 'sheet2'};
for q = 1 : length(file_list)
baseFileName = file_list{q};
sheetName = sheet_list{q};
thisInputFileName = sprintf('%s.xlsx', baseFileName);
if exist(thisInputFileName, 'file')
% Import data:
xlsx_file = xlsread(thisInputFileName, sheetName);
% Create filename for the output .mat file:
thisOutputFileName = sprintf('%s_%s.mat', baseFileName, sheetName);
% Export 'xlsx_file' variable contents to mat file:
save (thisOutputFileName, 'xlsx_file')
end
end