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!