Code to automatically generate filename from reading cell in an excel file

1 次查看(过去 30 天)
Hello, its been a while since I used MATLAB and I seem to be having problems.
What Im trying to do: To read an excel file (specific cells) > name file using a series of those cells.
ie cell 1 = C2, cell 2 = 2013_02_25, cell 3 = WE08GYD >> file saved as C2_2013_02_25_WE08GYD.pdf
The code i'm using:
[~,~,testcll] = xlsread('Book1.xlsx','Sheet2','A1'); % 'C2';
[~,~,testdate] = xlsread('Book1.xlsx','Sheet2','A2'); %'2013_07_07';
[~,~,testnumber] = xlsread('Book1.xlsx','Sheet2','A3'); %21;
testtype = 'FTP75';
[~,~,vehicle] = xlsread('Book1.xlsx','Sheet1','D1'); %'Y654GYD';
% filename = -['WARNING TEST FILE_' testcll(1,1)] % <<<< Failing
% filename = sprintf('WARNING TEST FILE_%s_%s_%s_%s_%s', testcll, testdate,
% testnumber, testtype, vehicle) % <<<< Failing
% filename = ['WARNING TEST FILE_', testcll, '_', testdate, '_',
% testnumber, '_', testtype, '_', vehicle] % <<<< Failing
saveas(tst2, filename, 'pdf')
I've tried several things with sprintf (i think thats what is supposed to be used) but it can't seem to deal with the imported information being in a cell
Any help would be appreciated.
  3 个评论
Matthew
Matthew 2013-4-18
编辑:Matthew 2013-4-18
Thanks this has helped but its not picking up all the information correctly ie some cells are coming out as squares?
ie WARNING TEST FILE_C2_2013_07_12_ [a square where test number should be]_FTPXX_XXXRM10
Matthew
Matthew 2013-4-18
Sorted the above issue I needed to use num2str.
Fixed code is now:
[~,~,testcll] = xlsread('Book1.xlsx','Sheet2','A1'); % 'C2';
[~,~,testdate] = xlsread('Book1.xlsx','Sheet2','A2'); %'2013_07_07';
[~,~,testnumber] = xlsread('Book1.xlsx','Sheet2','A3'); %21;
testtype = 'FTP75';
[~,~,vehicle] = xlsread('Book1.xlsx','Sheet1','D1'); %'Y654GYD';
filename = ['WARNING TEST FILE_' testcll{1,1} '_' testdate{1,1} '_' num2str(testnumber{1,1}) '_' testtype '_' vehicle{1,1}]
saveas(tst2, filename, 'pdf')
Only issue I have now is to work out if I can get MATLAB to read / change information in a excel file from C2 2013-02-25 to C2_2013_02_25, then id pretty much have all my issues fixed for now!

请先登录,再进行评论。

回答(1 个)

Iman Ansari
Iman Ansari 2013-4-18
filename = ['WARNING TEST FILE_C2_2013-02-25']
filename(filename=='-')='_'

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by