how to run 2 while loops in one script

3 次查看(过去 30 天)
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

采纳的回答

Cris LaPierre
Cris LaPierre 2021-6-17
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 个评论
Cris LaPierre
Cris LaPierre 2021-6-17
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 2021-6-18
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 =)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by