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

4 views (last 30 days)
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 Comments
Matthew
Matthew on 18 Apr 2013
Edited: Matthew on 18 Apr 2013
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 on 18 Apr 2013
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!

Sign in to comment.

Answers (1)

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

Products

Community Treasure Hunt

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

Start Hunting!