필터 지우기
필터 지우기

delete excel rows whose first column is 0

조회 수: 2 (최근 30일)
Tian Tian
Tian Tian 2018년 3월 5일
댓글: Tian Tian 2018년 3월 5일
Hi, I want to delete rows if the first column in this row is 0. I know there are some answers online, I tried actxserver, but somehow it didn't work. Here is my code:
excelfilename = 'A.xlsx';
sheet = 'Sheet1';
[num,txt,raw]=xlsread(excelfilename,'Sheet1');
columnA = num(:,1);
nrows=length(columnA);
for i=1:nrows
if (columnA(i,1)==0)
row=i;
excel = actxserver('Excel.Application');
workbook = excel.Workbooks.Open(file);
worksheet = workbook.Worksheets.Item(sheet);
worksheet.Rows.Item(row).Delete;
end
end
workbook.Save;
excel.Quit;
But I kept getting errors. May anyone kindly give me a hand? Many thanks in advance.
  댓글 수: 2
Bob Thompson
Bob Thompson 2018년 3월 5일
Are you trying to delete the rows in excel directly? Why not just delete them in your array and then rewrite the data to a new, or the same excel file?
Actxserver is very powerful, but is also significantly more complicated than standard MATLAB, and there isn't as much online information for it.
What kind of errors are you getting?
Tian Tian
Tian Tian 2018년 3월 5일
Thank you. Yes rewriting might be a good idea. I will try.

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

채택된 답변

YT
YT 2018년 3월 5일
편집: YT 2018년 3월 5일
I just created some test data (numeric/text) to try and remove the rows that contain zeros in the first column. Think this is one of the easiest ways to do it, without using actxserver.
clear all;
%%----------- creating test data -----------
%Creating values
values = {1, 2, 3 ;
0, 5, 'x';
4, 0, 2;
3, 8, 9};
%Write xlsx file
xlswrite('A.xlsx',values,'Sheet1');
% ----------- end creating test data -----------
%%----------- Reading test data -----------
%Read xlsx file
[N,T,RAW] = xlsread('A.xlsx');
%Find zeros in first column
ind = find(cell2mat(RAW(:,1))==0);
%Remove rows
N(ind,:) = [];
%New data saved to second sheet of same file
xlswrite('A.xlsx',num2cell(N),'Sheet2');
  댓글 수: 3
YT
YT 2018년 3월 5일
I don't really know how to remove the 1st sheet and rename it, but you can also just create an entirely new excel file and save the new data to that one (and delete the original data manually)
%New data saved to new file
xlswrite('A_new.xlsx',num2cell(N));
But if it was me, I would just keep the original data (you'll never know if you need it again).
Tian Tian
Tian Tian 2018년 3월 5일
Thank you so much!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by