필터 지우기
필터 지우기

How to remove NaN from data read from excel. Nan values only not rows or columns

조회 수: 67 (최근 30일)
I have read 4 columns of data from an excel file. some columns have more data than others. Nan values have populated the blank cells. However I need to plot each column of data and the Nan values are preventing this. How do i either import the data without the nan values or once i have imported it how do I get rid of the nan values? I cant use code that removes rows or columns as otherwise i will lose valuable data. this some code i have been trying:
[~,sheet_name]=xlsfinfo('barchart_F1.xlsx');
barchart=xlsread('barchart_F1.xlsx');
barchart = importdata('barchart_F1.xlsx');
barchart=rmmissing(barchart);
  댓글 수: 4
suzanne o'callaghan
suzanne o'callaghan 2021년 11월 29일
hi star, i have tried readmatrix and readtable and NANs are still populating the table
Johannes Hougaard
Johannes Hougaard 2021년 11월 29일
In MATLAB your array is not made up of individual values/cells/columns/rows. MATLAB is focused around matrices not around values, whereas in excel each cell is separate and has no relation to neighboring cells.
If you wish to mimic that in MATLAB each cell from excel should be it's own variable (and you don't want that for the data you've got)
The matrix you read is of size 18x4 you can't have a different number of rows in different columns.

댓글을 달려면 로그인하십시오.

답변 (1개)

chiyaan chandan
chiyaan chandan 2021년 11월 29일
clc
clear all
clf
%%
disp ('Select User input excel file ');
[filename, pathname] = uigetfile('*.*');
xfile1 = fullfile(pathname, filename);
Data= xlsread(xfile1);
Data(isnan(Data))=0; % replace the NaN with 0
%%
[m,n]=size(Data);
for i=1:n
figure(i)
bar(Data(:,i))
end
once we replcae the NaN with 0 at the end of the coloumns then we plaot the barchart for for each coloumns in a different figures or we can plot in single barchart.
  댓글 수: 1
Johannes Hougaard
Johannes Hougaard 2021년 11월 29일
NaN values are perfectly fine for making the bar chart, there is no need to replace them with 0.
The graphs will be identical whether you keep the NaN (missing values) or replace them with 0 (actual values) but all statistics (such as the mean, standard deviation etc.) will be wrong when replacing zeros.
tmp = rand(18,4)*2 + 7;
tmp(14:end,4) = missing;
tmp(7:end,1) = missing;
figure;
bar(tmp');
ah = gca;
ah.ColorOrder = jet(18);
mean_values = mean(tmp,1,"omitnan")
mean_values = 1×4
8.0884 8.1448 8.1048 8.1167
tmp(14:end,4) = 0;
tmp(7:end,1) = 0;
figure;
bar(tmp');
ah = gca;
ah.ColorOrder = jet(18);
mean_values = mean(tmp,1,"omitnan")
mean_values = 1×4
2.6961 8.1448 8.1048 5.8620

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by