how to save matlab file according to excel sheet name

1 次查看(过去 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.

采纳的回答

Image Analyst
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 个评论
JE
JE 2017-1-13
Thanks a lot Image Analyst. The code worked but I needed the variables to be in their own vectors so I modified it a little bit, but the sprintf function was what I was looking for.
The problem I am having now is that I cannot get the code to loop through to the next worksheet.. The workbook is the same, but data on sheet1 and sheet 2 are different. They have the same variable names however.
addpath('C:filepath')
% titles of .xlsx files to import
file_list={'filename1','filename1','filename1'};
sheet_list= {'sheet1', 'sheet2','sheet3'};
for q = 1:length(file_list);
baseFileName = file_list{q};
sheetName = sheet_list{q};
InputFile = sprintf('%s.xlsx',baseFileName);
xlsx_file = xlsread(InputFile, sheetName);
% Clearing & removing non-datapoints
xlsx_file(1:27,:)=[];
xlsx_file(isnan(xlsx_file))=0;
% Names of variables in .xlsx file.
name_list = {'time',...
'Distance',...
.......
};
% Importing datapoints from .xlsx file and assigning their variable
% names
for i = 1:length(name_list)
g = name_list{i};
eval(strcat(g,'=xlsx_file(:,',num2str(i),');'))
end
clear csv_file g i date_list file_list InputFile...
name_list q
OutputFile = sprintf('%s_%s.mat',baseFileName,sheetName);
save (OutputFile)
end
Do you see a reason why this would not loop to the next worksheet and save another .mat file with the appropriate name?
Image Analyst
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 个)

产品

Community Treasure Hunt

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

Start Hunting!

Translated by