how to put in zeros in the empty spaces of a matrix read by xlsread()?
1 次查看(过去 30 天)
显示 更早的评论
I have a xlsx data sheet which needs to be imported as a matrix. I am using xlsread for the same. But many spaces in the dataset are empty which are read as Nan. I want to replace these values with zeros. I have tried to initialize the matrix as:
a = magic(100); a = xlsread(filename.xlsx);
But still the same problem as I believe the matrix gets resized after the import.
Debanjan
0 个评论
采纳的回答
per isakson
2014-10-4
编辑:per isakson
2014-10-4
Try
M(isnan(M))=0;
 
In response to comments:
See the on-line documentation on xlsread. This is copied from one of its example
values = {1, 2, 3 ; 4, 5, 'x' ; 7, 8, 9};
headers = {'First', 'Second', 'Third'};
xlswrite('myExample.xlsx', [headers; values]);
[ndata, text, alldata] = xlsread('myExample.xlsx')
it returns
ndata =
1 2 3
4 5 NaN
7 8 9
text =
'First' 'Second' 'Third'
'' '' ''
'' '' 'x'
alldata =
'First' 'Second' 'Third'
[ 1] [ 2] [ 3]
[ 4] [ 5] 'x'
[ 7] [ 8] [ 9]
You are right ndata doesn't include the "header-rows", alldata does.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!