Reading from many columns of an excel sheet

I am using the following code to read from multiple columns of an excel sheet. Can someone help me to reduce this code?
ex = actxserver('excel.application');
ex.visible = 1;
exwb = ex.Workbooks.Open('C:\Sgpa.xls');
exSheet1=exwb.Sheets.Item('Marks');
exSheet2=exwb.Sheets.Item('Points');
%ex.Sheet1.Range.invoke;
for i=5:324
val = exSheet1.Range(['J',num2str(i)]).get('Value');
if isnumeric(val) && isfinite(val)
exSheet2.Range(['J',num2str(i)]).set('Value',Points(val));
end
val = exSheet1.Range(['K',num2str(i)]).get('Value');
if isnumeric(val) && isfinite(val)
exSheet2.Range(['K',num2str(i)]).set('Value',Points(val));
end
val = exSheet1.Range(['L',num2str(i)]).get('Value');
if isnumeric(val) && isfinite(val)
exSheet2.Range(['L',num2str(i)]).set('Value',Points(val));
end
val = exSheet1.Range(['M',num2str(i)]).get('Value');
if isnumeric(val) && isfinite(val)
exSheet2.Range(['M',num2str(i)]).set('Value',Points(val));
end
val = exSheet1.Range(['N',num2str(i)]).get('Value');
if isnumeric(val) && isfinite(val)
exSheet2.Range(['N',num2str(i)]).set('Value',Points(val));
end
val = exSheet1.Range(['O',num2str(i)]).get('Value');
if isnumeric(val) && isfinite(val)
exSheet2.Range(['O',num2str(i)]).set('Value',Points(val));
end
end
exwb.Save;
ex.Quit;
ex.delete;

Réponses (1)

Fangjun Jiang
Fangjun Jiang le 25 Nov 2011

0 votes

Can you explain why you don't use xlsread() directly?
A=rand(10); xlswrite('test.xls',A,'FirstSheet','B2');
B=xlsread('test.xls','FirstSheet','C3:E5');

5 commentaires

Unnikrishnan PC
Unnikrishnan PC le 25 Nov 2011
xlsread() reads an entire range into an array. My object is to read each cell data and operate on the data depending on the type of data using a function Points(val). Also my excel sheet is about 500 rows and 12 columns. Operating with such a big array might be difficult.Thanks for the comment. Please help if you have another solution.
Walter Roberson
Walter Roberson le 25 Nov 2011
500 by 12 is small. Less than 48 kilobytes of storage.
Image Analyst
Image Analyst le 25 Nov 2011
It's funny that students used to learning linear algebra with 3 by 4 matrices think that think sizes like 500x12 are huge. I guess they don't know that typical digital images are like 4000 by 5000 and even those are small. People using CT images, mass spec images, or seismic data sets can be around 100 gig or more. I've heard of some seismic data sets that are terabytes for one "image".
Unnikrishnan PC
Unnikrishnan PC le 25 Nov 2011
I tried xlsread() but it does not work.I am passing A to a function which will process the data. Please help
A = xlsread('C:\File.xls','Marks','A1:D10');
[rows cols] = size(A);
for m = 1:rows
for n = 1:cols
B(m,n)=A(m,n);
B(m,n)= Grades(A(m,n));
end
end
xlswrite('C:\File.xls',B,'Grades','A1:D10');
Fangjun Jiang
Fangjun Jiang le 26 Nov 2011
"does not work" really doesn't describe anything. See update. You may need to use [Num, Txt, Raw]=xlsread() for different types of data in the Excel file. All the rest should be straightforward. See doc for help.

Connectez-vous pour commenter.

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by