Filter löschen
Filter löschen

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

67 Ansichten (letzte 30 Tage)
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 Kommentare
suzanne o'callaghan
suzanne o'callaghan am 29 Nov. 2021
hi star, i have tried readmatrix and readtable and NANs are still populating the table
Johannes Hougaard
Johannes Hougaard am 29 Nov. 2021
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

chiyaan chandan
chiyaan chandan am 29 Nov. 2021
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 Kommentar
Johannes Hougaard
Johannes Hougaard am 29 Nov. 2021
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

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by