PhD Chapter - from A to Z in importing the data from XLS

(Comments)

So a couple of question that comes up when we want to deal with the data is how to read and deal with them. 

So I will take the example on how to read it, how to filter them, and how to clean from Nan.

1. How to locate your file

2. How to import in the shape that we want. 

3. The logic behind naming every row or column as the header

4. Use this data and be ready for any statistic that we want. 

How to locate your file. 

1. It's pretty simple actually. Just click on the current matlab directory that you have. The place is in the left panel of Matlab 

 There you can directly type the function to read the xls. 

And here is my data look like.

You can download the data from this link. 

%1. First is clean all the data 
% a clear MATLAB workspace is a clear mental workspace
close all; clear; clc

%2. Import with xls read
[num,txt,raw]= xlsread("state-marriage.xlsx")
data = raw;
%3. check sizes and outputs
whos

%4. Make the matrix from the data
% initialize matrices
%M is the matrix where there is 51 row with 23 column
M = zeros(51,23);
%statesM is creating the cell with 51 row and 1 column
statesM = cell(51,1);
%the year is creating 23 row with one column
yearM = zeros(23,1);

% 5. import the data; start with a loop over columns
% for the column i from 1 until 23
for coli = 1:length(yearM)

% get year (same for all rows...)
% some columns are text, others are numeric
% the symbol { meaning the new row
yearval = data{2,coli+1};
if isnumeric(yearval)
yearM(coli) = yearval;
else
yearM(coli) = str2double(yearval);
end



% loop over rows
for rowi = 1:51

% get value from this cell and convert to number
val = data{rowi+3,coli+1};
if isnumeric(val)
M(rowi,coli) = val;
else
M(rowi,coli) = str2double(val);
end


% get state label (only in first colum)
if coli==1
statesM{rowi} = data{rowi+3,1};
end
end % end row loop
end % end column loop

%% 6. Check if the data clean the marriage data

figure(1), clf
imagesc(yearM,[],M)
colorbar
set(gca,'clim',[0 10])
xlabel('Year'), ylabel('State')

% 7. Replace the Nan with something, for example median or zero
% replace with column zero
[nanrow,nancol] = find(isnan(M));
for i=1:length(nanrow)
M(nanrow(i),nancol(i)) = nanmedian(M(:,nancol(i)));
end

% And thats all

So that's all now the way to import excel file! Hopefully it helps! 

Currently unrated

Comments

Riddles

22nd Jul- 2020, by: Editor in Chief
524 Shares 4 Comments
Generic placeholder image
20 Oct- 2019, by: Editor in Chief
524 Shares 4 Comments
Generic placeholder image
20Aug- 2019, by: Editor in Chief
524 Shares 4 Comments
10Aug- 2019, by: Editor in Chief
424 Shares 4 Comments
Generic placeholder image
10Aug- 2015, by: Editor in Chief
424 Shares 4 Comments

More News  »

Fixing the issue in assumption of OLS step by step or one by one

Recent news

Hi, I want to raise the issue related to know whether your OLS is ok or not. 

read more
5 days, 20 hours ago

Meaning of 45 degree in economics chart

Recent news

The **45-degree line** in economics and geometry refers to a line where the values on the x-axis and y-axis are equal at every point. It typically has a slope of 1, meaning that for every unit increase along the horizontal axis (x), there is an equal unit increase along the vertical axis (y). Here are a couple of contexts where the 45-degree line is significant:

read more
1 month, 1 week ago

hyperinflation in hungary

Recent news

The **hyperinflation in Hungary** in the aftermath of World War II (1945–1946) is considered the worst case of hyperinflation in recorded history. The reasons behind this extreme economic event are numerous, involving a combination of war-related devastation, political instability, massive fiscal imbalances, and mismanagement of monetary policy. Here's an in-depth look at the primary causes:

read more
1 month, 2 weeks ago

what is neutrailty of money

Recent news

**Neutrality of money** is a concept in economics that suggests changes in the **money supply** only affect **nominal variables** (like prices, wages, and exchange rates) and have **no effect on real variables** (like real GDP, employment, or real consumption) in the **long run**.

read more
1 month, 2 weeks ago

Japan deflationary phenomenon

Recent news

Deflation in Japan, which has persisted over several decades since the early 1990s, is a complex economic phenomenon. It has been influenced by a combination of structural, demographic, monetary, and fiscal factors. Here are the key reasons why deflation occurred and persisted in Japan:

read more
1 month, 2 weeks ago

What the tips against inflation

Recent news

Hedging against inflation involves taking financial or investment actions designed to protect the purchasing power of money in the face of rising prices. Inflation erodes the value of currency over time, so investors seek assets or strategies that tend to increase in value or generate returns that outpace inflation. Below are several ways to hedge against inflation:

read more
1 month, 2 weeks ago

Long and short run philip curve

Recent news

The **Phillips Curve** illustrates the relationship between inflation and unemployment, and how this relationship differs in the **short run** and the **long run**. Over time, economists have modified the original Phillips Curve framework to reflect more nuanced understandings of inflation and unemployment dynamics.

read more
1 month, 2 weeks ago

How the government deal with inflation (monetary and fiscal) policies

Recent news

Dealing with inflation requires a combination of **fiscal and monetary policy** tools. Policymakers adjust these tools depending on the nature of inflation—whether it's **demand-pull** (inflation caused by excessive demand in the economy) or **cost-push** (inflation caused by rising production costs). Below are key approaches to controlling inflation through fiscal and monetary policy.

read more
1 month, 2 weeks ago

More News »

Generic placeholder image

Collaboratively administrate empowered markets via plug-and-play networks. Dynamically procrastinate B2C users after installed base benefits. Dramatically visualize customer directed convergence without