how to put in zeros in the empty spaces of a matrix read by xlsread()?

2 ビュー (過去 30 日間)
Debanjan Dasgupta
Debanjan Dasgupta 2014 年 10 月 4 日
コメント済み: Debanjan Dasgupta 2014 年 10 月 4 日
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

採用された回答

per isakson
per isakson 2014 年 10 月 4 日
編集済み: per isakson 2014 年 10 月 4 日
Try
M(isnan(M))=0;
&nbsp
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.
  4 件のコメント
Debanjan Dasgupta
Debanjan Dasgupta 2014 年 10 月 4 日
Thankx a lot per isakson. But after these steps, the size function is giving me errors. command used: rows = size(a, 1) Error: Subscript indices must either be real positive integers or logicals.
Error in buzsakiIInetwork_coherence_multifiles_multirows (line 26) rows = size(a, 1)
Debanjan Dasgupta
Debanjan Dasgupta 2014 年 10 月 4 日
I could do it.. Actually there was some old variable size in the workspace but not in my script which was creating the trouble... Thankx a lot guys for all the help.. :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLanguage Support についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by