How to Save a raw array and delete NaN in a raw array

3 ビュー (過去 30 日間)
Ibro Tutic
Ibro Tutic 2015 年 10 月 8 日
コメント済み: Ibro Tutic 2015 年 10 月 8 日
I am using xlsread to get data from .csv files. I need all of the information from these files, so the [raw] = xlsread(thisfile), it what I need to use. However, how would I go about saving this array as a .mat file and replacing any NaN values with empty cells?
I need to use the raw data, unless somebody else knows a way to get all text/numerical data into the array. I tried using textscan, but it was far to complicated.

採用された回答

TastyPastry
TastyPastry 2015 年 10 月 8 日
arr(cellfun(@(x) any(isnan(x)),arr)) = {[]};
Where arr is your input array.
  3 件のコメント
TastyPastry
TastyPastry 2015 年 10 月 8 日
As Stephen said, you can't have an "empty" cell in a cell array. What you replace NaN with depends on what you're trying to do with the data. I think the empty vector is probably your best bet, since if you're trying to index out whatever cells are "empty", you could use isempty to find the empty cells.
Ibro Tutic
Ibro Tutic 2015 年 10 月 8 日
Thanks for the help!

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

その他の回答 (1 件)

Stephen23
Stephen23 2015 年 10 月 8 日
編集済み: Stephen23 2015 年 10 月 8 日
In MATLAB there is no such thing as an empty cell. Every cell contains something, even if it just an empty array. Initializing a cell array using cell places empty arrays in each cell. You could:
  • replace the contents of those cells with empty arrays.
  • delete those cells.
  • keep an index of which cells to ignore.
Whatevery way you create a cell array, the cells always contain another array:
>> X = cell(1,3)
X =
[] [] []
>> X{3}
ans =
[]
>> size(X{3})
ans =
0 0
>> Y{3} = []
Y =
[] [] []
  2 件のコメント
Ibro Tutic
Ibro Tutic 2015 年 10 月 8 日
So there is no way to replace the NaN's with empty cells? Is there a way I could combine the number and text arrays and use that?
Stephen23
Stephen23 2015 年 10 月 8 日
編集済み: Stephen23 2015 年 10 月 8 日
I think the neatest and most robust solution would be to not change your cell contents at all and simply create a separate vector/array of indices that lets you keep track of those cells. This follows the programming best practice of minimizing changes to raw data.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by