How to read data from xls file in loop?

2 views (last 30 days)
Shashank Katiyar
Shashank Katiyar on 30 Jan 2013
Edited: Walter Roberson on 21 Dec 2016
I have one matrix("dummy") with some data in it. I want to sort that data and append it in a matrix which already has some data in it according to the relation satisfied by data. For that I want to run a loop which will read data and check the condition and then append it in the corresponding cell of file. I have written following code.
offset = ['A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z'];
maxload = 0; index_1 = 0; index_2 = 0; alpha = offset(1); temp = dummy(1,1); N_offset = 1;
for i = 1:n(1,1)
for j = 1:m(1,1)
if(temp >= dummy(j,i) && maxload <= 100)
temp = dummy(j,i);
index_1 = i;
index_2 = j;
end
maxload = maxload+delnode(4,i); %add condition for delivery node also
end
for k = 1:m(1,1)
columnB = xlsread('cluster.xls',1,'B%d',N_offset);
N_offset = N_offset+1;
if (index_2 == k)
xlswrite('cluster.xls', delnode(:,index_1), 1, sprintf('%c1',(alpha)));
alpha = offset(1+k);
end
end
end
But I get the following error.
Warning: Import mode string is invalid. XLSREAD resets mode to normal.
> In xlsread at 170
In VRP_ver2 at 73
Error using xlsreadCOM (line 48)
Data range is invalid.
Error in xlsread (line 230)
[numericData, textData, rawData, customOutput] = xlsreadCOM(file, sheet, range, Excel,
customFun);
Error in VRP_ver2 (line 73)
columnB = xlsread('cluster.xls',1,'B%d',N_offset);
Please help me in resolving this issue.
Thank You
Shashank Katiyar

Answers (2)

Jing
Jing on 30 Jan 2013
Could you please format your code? it's hard to read!
The error is in xlsread line (line 73), for 'B%d', I think you should write ['B' num2str(N_offset)] in this line.

Image Analyst
Image Analyst on 30 Jan 2013
You can't do this:
xlsread('cluster.xls',1,'B%d',N_offset);
You need to create the cell reference as a string, for example with sprintf():
cellReference = sprintf('B%d', N_offset);
columnB = xlsread('cluster.xls',1, cellReference);
I don't know how many times you're calling xlsread, but if it's more than 2 or 3 times, you really should consider using ActiveX. It will be A LOT faster since you will only launch and shut down Excel once instead of with every call to xlsread().

Products

Community Treasure Hunt

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

Start Hunting!