how to run 2 while loops in one script

2 Ansichten (letzte 30 Tage)
Hüseyin Uzun
Hüseyin Uzun am 17 Jun. 2021
Kommentiert: Hüseyin Uzun am 18 Jun. 2021
i have 2 scripts each with a while loop. the first script collects data every 10 minutes and the second script plots the collected data once a day. I have found that I cannot run two scripts at the same time in Matlab. Unless I install two different Matlab versions or I use the parallel computing tollbox, right?
Is there a way to run both while loops in one script?
unfortunately I can't attach the .dat1 file but I have attached the datalogger file from the second script.
Skript 1:
while true
tic
data=importdata('C:\Users\Hüs\Documents\MATLAB\Datalogger\KlimaLoggProAuto.dat1');
alle=[data(8,1);data(11,1);data(21,1);data(24,1);data(34,1);data(37,1);data(47,1);data(50,1);data(60,1);data(63,1);data(73,1);data(76,1);data(86,1);data(89,1);data(99,1);data(102,1)];
string(alle);
a=split(alle,'"');
Werte=a(:,2);
Werte=Werte';
Zeit=datetime('now');
Zeit=string(Zeit);
Werte=[Zeit Werte]; %the variable 'Werte' appears to change size on every loop iteration.
% Consider preallocating for speed. An other question: what can i do about this?
Werte=cellstr(Werte);
[succes,message]=xlsappend('Datalogger.xlsx',Werte,1);
pause(600-toc)
end
Skript 2:
while true
tic
table=readtable('C:\Users\Hüs\Documents\MATLAB\Datalogger\Datalogger.xlsx');
warning('off','all');
x=table.DatumUndUhrzeit; %Datum und Uhrzeit auslesen
x=x(end-468:end);
time=datetime(datestr((x)));
%Raum 210
temp210=table.Temp_R_210__C; %Temperatur Raum210 auslesen
temp210=temp210(end-468:end);
temp210=str2double(temp210);
hum210=table.Hum___; %Luftfeuchtigkeit Raum210 auslesen
hum210=hum210(end-468:end);
hum210=str2double(hum210);
f210=figure('Name','Room210','visible','off');
hold on;
yyaxis left;
plot(time,temp210);
yyaxis right;
plot(time,hum210);
yyaxis left;
title('Temperature and Humidity Room 210');
ylabel('Temperature in °C');
yyaxis right;
ylabel('Humidity in %');
xtickangle(45),
ax = gca();
ax.XTick = linspace(ax.XTick(1),ax.XTick(end),15);
saveas(f210,'Room210.jpg');
hold off;
end

Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 17 Jun. 2021
It seems like this could be written in a single while loop with an if statement that only executes the code corresponding to the entire day after a specified number of 10 minute intervals have been collected.
  3 Kommentare
Cris LaPierre
Cris LaPierre am 17 Jun. 2021
You should probably add a counter that increases every time the 10 minute code is executed. Once it equals 6*24, then run the daily code and reset the counter.
Something like this (untested).
run = 1;
while true
tic
data=importdata('C:\Users\Hüs\Documents\MATLAB\Datalogger\KlimaLoggProAuto.dat1');
...
[succes,message]=xlsappend('Datalogger.xlsx',Werte,1);
if run >= 144
table=readtable('C:\Users\Hüs\Documents\MATLAB\Datalogger\Datalogger.xlsx');
...
saveas(f210,'Room210.jpg');
hold off;
run = 0
end
run = run+1;
pause(600-toc)
end
Hüseyin Uzun
Hüseyin Uzun am 18 Jun. 2021
Thanks a lot!
I tried it with small numbers and it worked. I just had to add +1 to the run>=144, because when i start the script it immediately exectues one run.
I tested it this way:
run = 1;
while true
tic
data=importdata('C:\Users\Hüs\Documents\MATLAB\Datalogger\KlimaLoggProAuto.dat1');
...
[succes,message]=xlsappend('Datalogger.xlsx',Werte,1);
if run >= 4 % only 4 runs
table=readtable('C:\Users\Hüs\Documents\MATLAB\Datalogger\Datalogger.xlsx');
...
saveas(f210,'Room210.jpg');
hold off;
run = 0
end
run = run+1;
pause(30-toc) % only 30 Secends
end
After 1:30 min the second part of the script started and created a new figure. I deleted the figure and let it run.
At min 3:30 it again created a figure and i deleted it again, to see that at 5:30 the figure again appears.
So everything works reliable =)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by