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?

31 views (last 30 days)
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

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 28 Apr 2020
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
Sebastián Terán
Sebastián Terán on 8 Jul 2022
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.

More Answers (2)

Erhan erhnbgdtl
Erhan erhnbgdtl on 28 Apr 2020

Erhan erhnbgdtl
Erhan erhnbgdtl on 28 Apr 2020
Ohh my good. Problem is arduino code. Serial.println("derece"). Thank you so so much.

Categories

Find more on Simulink Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!