필터 지우기
필터 지우기

how to save matlab file according to excel sheet name

조회 수: 1 (최근 30일)
JE
JE 2017년 1월 13일
댓글: Image Analyst 2017년 1월 14일
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개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by