How do we replacing empty or (NaN) cells with 0's in Matlab?

39 次查看(过去 30 天)
Dear experiences
i have a dataset stored in an excel file where some cells doesn't contain any values, therefore when importing or reading this data and stored it either in table or matrix .. empty cells either places in it NaN or still contain nill values .. so
first: i would like to replacing [] or NaN cells by 0's is there any code or function can do that please, i would thanks any one can give me a solution for this issue, for example when use :
data=xlread(filename), data will be view as follow
second: when importing this data using read table function, some columns are fully empty is there any function can be used to delete columns or rows if its never contain any values.. as shown in 2nd figure ..
thanks for any suggestion
---- table----
  2 个评论
Image Analyst
Image Analyst 2017-1-16
State what kind of data you'd prefer: A numerical matrix, or a table. And attach your workbook so we can try things.
ahmed obaid
ahmed obaid 2017-1-18
ok, thanks for your interesting attached a portion data in an excel file

请先登录,再进行评论。

回答(2 个)

dpb
dpb 2017-1-16
doc isnan
doc isempty
doc all
Look for "logical addressing"

Niels
Niels 2017-1-16
编辑:Niels 2017-1-16
A(isnan(A))=0 % turns every NaN to zeros
to "delete" a row set the row =[]
if A(i,:)==0
A(i,:)=[]
you could use a for loope, but if you do,start from the last row
  2 个评论
Guillaume
Guillaume 2017-1-16
@Niels, never ever recommend to beginners the syntax
if somevector
%do something
end
Always wrap the vector in any or all depending on what is desired. There are plenty of questions on Answer which are due to the person not knowing what if does when passed a vector. In the above case:
if all(A(i, :) == 0)
A(i, :) = [];
end
And actually, do you know, without looking up the doc or testing in matlab what the output of the following is?
if [true false]
disp('test 1 passed');
end
if [true true]
disp('test 2 passed');
end
if [false false]
disp('test 3 passed');
end
if [true true true true true true false true true]
disp('test 4 passed');
end

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by