MATLAB: I want to interface with the DHT11 sensor in matlab.But program give this error Subscripted assignment dimension mismatch. Error in Untitled (line 27) Humi(i)=st​r2num(out(​5:9)); Can you help me

dht11 interface

clc
clear all
delete(instrfindall);
close(instrfindall);
s = serial('COM3');
time=100;
i=1;
while(i<time)
fopen(s)
fprintf(s, 'Your serial data goes here')
out = fscanf(s)
Temp(i)=str2num(out(1:4));
subplot(211);
plot(Temp,'g');
axis([0,time,20,50]);
title('Parameter: DHT11 Temperature');
xlabel('---> time in x*0.02 sec');
ylabel('---> Temperature');
grid
Humi(i)=str2num(out(5:9));
subplot(212);
plot(Humi,'m');
axis([0,time,25,100]);
title('Parameter: DHT11 Humidity');
xlabel('---> time in x*0.02 sec');
ylabel('---> % of Humidity ');
grid
fclose(s)
i=i+1;
drawnow;
end
delete(s)
clear s

Best Answer

  • Erhan - so the out variable is a string with value '23.00derece'. Your line of code
    Humi(i)=str2num(out(5:9));
    extracts chracters 5 through 9 which is
    0dere
    and then converts it to a number which, because of the non-numeric characters, is an empty array. What are you expecting in characters 5 through 9?