How do i refer to a column in my imported data and how do i turn all the NaN's into 0's?

5 views (last 30 days)
This is what I started: [numbers,string,raw]=xlsread('mortgagedata',2);
now if I wanted to name each column x1, x2, x3,...x23, how to I do this?
Also the empty cells have been named 'NaN' how do I turn these into e.g. 0 or assign them a letter?
Thanks in advance

Answers (2)

KSSV
KSSV on 9 Mar 2017
Edited: KSSV on 9 Mar 2017
Why there is necessity to name very column x1,x2,......x23 when the data is available in matrix form numbers? You can access your required column using numbers(:,1), numbers(:,3),...etc.
Replacing Nan's with zero:
A = [NaN 1 2 ; NaN NaN 3 ; 4 5 6]
A(isnan(A)) = 0 ;
  4 Comments
mabdorab
mabdorab on 9 Mar 2017
ok I have understood how to refer to the different columns but now I am confused again. sorry
[numbers,string,raw]=xlsread('mortgagedata');
numbers(isnan(numbers))=0 string(isnan(string))=0 %ERROR ON THIS LINE raw(isnan(raw))=0

Sign in to comment.


Guillaume
Guillaume on 9 Mar 2017
Do not use numbered variables. It does not simplify things later, it makes them more complicated. Keep your data as a matrix. Everytime you want x1, simply use numbers(:, 1). If you create numbered variables, you can't easily apply the same operation to several variables/columns, you just pollute the workspace with unnecessary variables, and you lose a lot of genericity.
Additionally, since the now ancient R2013b, you can have matrices with named columns in the form of tables. It makes it easy to access columns and it is also much easier to import excel files:
t = readtable('mortgagedata', 2, 'ReadVariableNames', false)
%and if your spreadsheet has a header, change 'ReadVariableNames' to true
%and it'll use the header to name the variables
t.Properties.VariableNames = arrayfun(@(n) sprintf('x%d', n), 1:t.width, 'UniformOutput', false);
Replacing NaN or missing values is also trivial since R2016b:
t = fillmissing(t, 'constant', 0);
The above will work in just one line with a table or matrix but if you create 23 variables, you'll have to type it 23 times. See how creating numbered variables doesn't simplify anything.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!