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?

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

5 Comments

Erhan - why 5:9 in
Humi(i)=str2num(out(5:9));
? Perhaps out isn't exactly what you think it is. I recommend using the MATLAB debugger to check to see what out is and what out(5:9) becomes. It could be two numbers (separated by a space?) that creates an array of two or more elements via str2num.
What is the out variable set to? Either use the MATLAB debugger to determine the value or print it out with
out = fscanf(s)
fprintf('out is set to : %s\n', out);

Sign in to comment.

 Accepted 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?

1 Comment

Codigo de arduino:
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
delay(2000);
dht.begin(); // initialise DHT11 sensor
}
void loop() {
float temp = dht.readTemperature(); //read temperature data
float humi = dht.readHumidity(); //read temperature data
Serial.print(temp);
Serial.print(humi);
delay(1000);
}
Mal:
Serial.println(temp);
Serial.println(humi);
Bien:
Serial.print(temp);
Serial.print(humi);

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!