how to run 2 while loops in one script
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
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
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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Whos finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!