Excel Reading and Writing using Matlab

I have to read data from a sheet. I have to read each cell and if it contains a number process it and write it to another sheet. How can I change the reference of cells using a loop so that I can read cells one after the other.

 채택된 답변

Friedrich
Friedrich 2011년 8월 19일

1 개 추천

Hi,
I have an Excel file where the first column (A) is:
1
2
e
3
and loop over the cells and check if the entry is a number. If so I write it into column B.
ex = actxserver('excel.application');
ex.visible = 1;
wb = ex.Workbooks.Open('C:\Users\fhempel\Desktop\tmp\Mappe1.xlsx');
for i=1:10
val = ex.Range(['A',num2str(i)]).get('Value');
if isnumeric(val)
ex.Range(['B',num2str(i)]).set('Value',val);
end
end
wb.Save;
ex.Quit;
ex.delete;

댓글 수: 3

Unnikrishnan PC
Unnikrishnan PC 2011년 8월 19일
Dear Friedrich,
Thanks for the code and help. Your code is nice but copies NULL when it see a character. I do not like to do anything if the cell content is a character or null. See eg:-
Col: A
1
2
e
NULL
5
Column B(Before)
10
20
30
40
50
Column B(After)
1
2
30
40
5
Thanks in Advance
UK
Friedrich
Friedrich 2011년 8월 19일
The code do nothing if its sees a char or NULL. Thats why Column B keeps the 30 and 40 since e and NULL are skipped and nothing is done with Column B. Thats handled by the part with if isnumeric(val). Do you like to overwrite the content if you see a char? If so add a else brace in which you do ex.Range(['B',num2str(i)]).set('Value',[]);
Unnikrishnan PC
Unnikrishnan PC 2011년 8월 19일
Thank You Very Much

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2011년 8월 19일

0 개 추천

Hi Unnikrishnan PC!
Hi Friedrich!
added variant:
[n t]=xlsread('eg1',1)
xlswrite('eg1', n(~isnan(n)), 2)

카테고리

Community Treasure Hunt

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

Start Hunting!

Translated by