how to save matlab file according to excel sheet name
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Image Analyst
2017-1-13
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
2 个评论
Image Analyst
2017-1-14
Rather than guessing, use xlsfinfo() to get a list of the actual sheet names in the workbook.
Don't use eval(), ever. No need to.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!