Hi Guys,
Does anyone know how to remove zeros while keeping the zero that is at the beginning and end of a value from excel sheet to make figure 1 to look like figure 2.
Thank you!

댓글 수: 6

Walter Roberson
Walter Roberson 2019년 2월 7일
You need to describe why those particular 0 0 should be preserved, what rule to use. The rest is easy .
Guillaume
Guillaume 2019년 2월 7일
편집: Guillaume 2019년 2월 7일
Also, if you don't know how to do that in matlab, why do you want to do it in matlab? You may as well write the code in VBA and do it all in excel. It would be faster
LIM JIAXIN
LIM JIAXIN 2019년 2월 7일
those particular 0 show in figure 2 is preserved for plotting the figure in 3D. Is there any ways to preserve those 0 that is at the beginning and end of the column value while removing the rest of the unwanted 0. I am quite new to this, thank you.
LIM JIAXIN
LIM JIAXIN 2019년 2월 7일
@guillaume the reason i'm doing this in matlab is i have some knowledge compare to VBA (0 knowledge). Would appreciate if i can get some help here instead.
Walter Roberson
Walter Roberson 2019년 2월 7일
II am not clear on why the 0 is not preserved on top of the column with 4s?
Bob Thompson
Bob Thompson 2019년 2월 7일
편집: Bob Thompson 2019년 2월 7일
There are two 0s on top of the 4s, only the adjacent one is retained.
At least that is what it looks like to me.

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

 채택된 답변

Guillaume
Guillaume 2019년 2월 7일

1 개 추천

No loop, no cellfun:
filetoedit = 'C:\somewhere\somefile.xlsx';
[data, ~, ascell] = xlsread(filetoedit);
ascell(~conv2(data ~= 0, [1; 1; 1], 'same')) = {[]}; %the conv2(data ~= 0, [1; 1; 1], 'same') expands non-zeros one row up and down. The remaining 0 are replaced by []
xlswrite(filetoedit, ascell);

추가 답변 (1개)

Bob Thompson
Bob Thompson 2019년 2월 7일

0 개 추천

Guillaume and Walter will probably have much better methods for doing this, but here is my first thought.
[num,txt,data] = xlsread('myfile.xlsx'); % Read the excel file
for i = 1:size(data,1); % Loop through rows
for j = 1:size(data,2); % Loop through columns
if i > 1 & i < size(data,1) & data{i,j} == 0 & data{i+1,j} == 0 & data{i-1,j} == 0
data{i,j} = [];
elseif i == 1 & data{i,j} == 0 & data{i+1,j} == 0
data{i,j} = [];
elseif i == size(data,1) & data{i,j} == 0 & data{i-1,j} == 0
data{i,j} = [];
end
end
end
Unfortunately, I wasn't able to find a method that didn't involve loops, but I'm also not very good with cellfun.

댓글 수: 1

Guillaume
Guillaume 2019년 2월 7일
cellfun is just a loop in disguise, often slower. What you gain with cellfun is guarantee that the output is the correct size and clarity (in my opinion) of the code. cellfun is also an example of functional programming which may be preferred over the imperative nature of loops.
Neither loops, nor cellfun are needed for this however.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

태그

질문:

2019년 2월 7일

답변:

2019년 2월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by