How to replace char 'null' in number 0?

Hi! I`ve got a problem with using plot function cause there is some 'null's (char type) in my data files . So i would like to change/replace that nulls in 0 (num) . Could someone help to find useful function or to offer other solution.

6 comentarios

Bizzy Dy
Bizzy Dy el 9 de Ag. de 2018
how it set in tsv data file
Guillaume
Guillaume el 9 de Ag. de 2018
It would be much simpler to fix the way you're reading the file rather than trying to fix the problem after the fact.
For that we would need to see the code you're using (not a screenshot, an actual copy as text) and a sample file (again not a screenshot!)
Bizzy Dy
Bizzy Dy el 9 de Ag. de 2018
Editada: Bizzy Dy el 9 de Ag. de 2018
%%read
clear all, close all;
h=tdfread('result_3259_m.tsv','tab')
%%get
time=getfield(h,'time')
Qus=getfield(h,'input0x2Eflowrate0x2Eflowmeter')
P1=getfield(h,'input0x2Epressure0x2Ebefore')
P2=getfield(h,'input0x2Epressure0x2Eafter')
P3=getfield(h,'input0x2Epressure0x2EforFlowmeter')
T1=getfield(h,'input0x2Etemperature0x2Ebefore')
T2=getfield(h,'input0x2Etemperature0x2Eafter')
A=getfield(h,'input0x2Evalve0x2Eposition')
% bezm=getfield(h,'BEZM')
% rmse=getfield(h,'RMSE')
%%dateformat
formatIn='yyyy-mm-ddTHH:MM:ssZ'
% ds=datetime(time,'InputFormat','yyyy-mm-ddTHH:MM:ssZ')
dn=datenum(time,formatIn)
%%draw
dateborder1=datenum(2018,07,01,0,0,0)
dateborder2=datenum(2018,07,26,0,0,0)
subplot(2,1,1);
plot(dn,P1,'y');hold on;
plot(dn,P2,'c');hold on;
plot(dn,P3);hold on;
plot(dn,T1,'m');hold on;
plot(dn,T2,'g');hold on;
plot(dn,A,'r');hold on;
datetick('x','dd.mm, HH:MM','keepticks')
datetickzoom
axis([dateborder1, dateborder2, -20 , 100])
legend('P1','P2','P3','T1','T2','A')
grid on
% grid minor
subplot(2,1,2);
% plot(dn,bezm);hold on;
plot(dn,Qus,'r');hold on;
% plot(dn,rmse,'g');hold on;
datetick('x','dd.mm, HH:MM','keepticks')
datetickzoom('x',1)
% xticks(0:10:100)
axis([dateborder1, dateborder2, 0 , max(Qus)])
% legend('Bezm','Qus')
grid on
% grid minor
Stephen23
Stephen23 el 9 de Ag. de 2018
Editada: Stephen23 el 9 de Ag. de 2018
Guillaume is right: fix the problem when it occurs, rather than trying to write hack code to "fix" it later. Upload a sample data file by clicking the paperclip button.
Bizzy Dy
Bizzy Dy el 9 de Ag. de 2018
done
Guillaume
Guillaume el 9 de Ag. de 2018
Please do not close questions that have been answered.

Iniciar sesión para comentar.

 Respuesta aceptada

Guillaume
Guillaume el 9 de Ag. de 2018
Editada: Guillaume el 9 de Ag. de 2018
It's much easier to read your file with readtable (which also does not require any toolbox). It is trivial with readtable to convert the 'null' to 0 and read everything else as number.
t = readtable('result_3259_m.tsv', 'FileType', 'text', 'TreatAsEmpty', 'null', 'EmptyValue', 0);
t.time = datetime(t.time, 'InputFormat', 'yyyy-MM-dd''T''HH:mm:ss''Z''');
For further processing you may even want to convert the table to a timetable
I would also recommend that you do not extract the table columns into variables with meaningless names. Just use the table variables directly:
plot(t.time, t.input_flowrate_bezm, 'DatetimeTickFormat', 'dd.MM, HH:mm');

Más respuestas (1)

Ameer Hamza
Ameer Hamza el 9 de Ag. de 2018

0 votos

The image shows a struct which char arrays as fields. You can use strrep() to replace the elements of a char array. Although it will still not work with the plot() function because the elements are char arrays, not numeric values. You might need to use str2num() to convert char values t numeric before using plot().

Productos

Versión

R2017a

Etiquetas

Preguntada:

el 9 de Ag. de 2018

Comentada:

el 9 de Ag. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by