Go through the table with a loop and change values

20 次查看(过去 30 天)
I have 30 columns and there are values in these columns.
  2 个评论
BN
BN 2020-2-12
编辑:BN 2020-2-12
Hello, Do you want to replace -9 to NaN across your table? I mean You want to change every -9 in your table to NaN?

请先登录,再进行评论。

回答(3 个)

Subhadeep Koley
Subhadeep Koley 2020-2-12
编辑:Subhadeep Koley 2020-2-12
ds = record ("xlsfile", "dataset.csv");
data = dataset2table(ds);
[rows, cols] = size(data);
newData = data;
for i = 1: rows
for j = 1: cols
if table2array(data(i, j)) == -9
newData(i, j) = array2table(NaN);
end
end
end
  7 个评论
Megan
Megan 2020-2-12
the empty rows are coded automatically as NaN in Matlab.
In my Questionnare -9 also means Error so, I want to change -9 into NaN
Subhadeep Koley
Subhadeep Koley 2020-2-12
Your "dataset.csv" is encoded with UTF-16-LE, which is not fully supported by the function readtable. Therefore, I copied and pasted all the data in a new .xlsx file (attached here).
The below code might be helpful now although it is not a very efficient solution.
clc;
data = readtable('Book1.xlsx');
[rows, cols] = size(data);
newData = data;
for i = 1: rows
for j = 1: cols
temp = table2array(data(i, j));
if iscell(temp)
temp = cell2mat(temp);
end
if temp == -9
newData(i, j) = array2table(NaN);
end
end
end

请先登录,再进行评论。


Steven Lord
Steven Lord 2020-2-12
The standardizeMissing function can accept arrays of various types, including table arrays and timetable arrays. If you only want to standardize the form in which missing data is stored for certain variables in your table you can tell it to only operate on specific 'DataVariables' as well.

BN
BN 2020-2-12
编辑:BN 2020-2-12
I think you won't need to use for loop. If A is the name of the table, then you can just use:
A= readtable('dataset.csv');
A{:,:}(A{:,:}==-9) = NaN
  9 个评论
BN
BN 2020-2-12
Oh yes I'm sorry I had a typo, use this:
A= readtable('dataset.csv');
A2 = table2array(A);
A2(A2==-9) = NaN;
Megan
Megan 2020-2-12
No, it did not work :(
Undefined operator '==' for input arguments of type 'cell'.
Error in analysis (line 25)
A2(A2==-9) = NaN;

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by