Clear Filters
Clear Filters

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

21 views (last 30 days)
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 Comments
Image Analyst
Image Analyst on 16 Jan 2017
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 on 18 Jan 2017
ok, thanks for your interesting attached a portion data in an excel file

Sign in to comment.

Answers (2)

dpb
dpb on 16 Jan 2017
doc isnan
doc isempty
doc all
Look for "logical addressing"

Niels
Niels on 16 Jan 2017
Edited: Niels on 16 Jan 2017
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 Comments
Guillaume
Guillaume on 16 Jan 2017
@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

Sign in to comment.

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!