Update variables across workers using batch

2 views (last 30 days)
I am using Matlab to do some data aquisition. One of the problems I have run into is that Matlab can only do one thing at a time. I would like to use batch to do data logging and updating of some values on the gui. I cannot include my actual code as the code calls machines that most people will not have. Instead I am including pseudo-code that just does something random that I can translate to my actual code later.
I was able to do logging by saving in the main program then loading in the batch file. This probably is not the ideal solution but for what I want to do it works. In the following code "Test_Button" and "Test_Button2" can not run at the same time. If you click one the other waits. Sending these to run with seperate batch jobs seems like it should work but I have not been able to figure out how to update the figure with the current values from the batch programs.
The main issue in the code below is in the callback function "Logger" which I would like to update the figure as it runs. It doesn't get a error for BatchDisplay.String not exsiting but it also doesn't update it.
If there is something other than batch() that can run a simple program on a single core and allow other things to run I would be happy to try it. But batch() is the only thing I have found that does what I want.
function Tester
ExpandHeight=20;
f = figure('Visible','off','Position',[200,50,150,200+ExpandHeight]);
la=10;
Top=150+ExpandHeight; %The +250 is from changing the size of the figure;
H20=20;z20=0;H30=30;z30=0;
CurrTemp=0;
TestButton = uicontrol('Style','togglebutton','String','Get Random 1','Position',[la,Top-z20*(H20+5)-z30*(H30+5),100,H30],'Callback',@Test_Button);
z30=z30+1;
TempUp = uicontrol('Style','text','String',CurrTemp,'Position',[la,Top-z20*(H20+5)-z30*(H30+5),40,H20]);
z20=z20+1;
TestButton2 = uicontrol('Style','togglebutton','String','Get Random 2','Position',[la,Top-z20*(H20+5)-z30*(H30+5),100,H30],'Callback',@Test_Button2);
z30=z30+1;
TempUp2 = uicontrol('Style','text','String',CurrTemp,'Position',[la,Top-z20*(H20+5)-z30*(H30+5),40,H20]);
z30=z30+1;
LogButton = uicontrol('Style','togglebutton','String','Starting Job...','Position',[la,Top-z20*(H20+5)-z30*(H30+5),100,H30],'Callback',@Log_Button);
z20=z20+1;
BatchDisplay = uicontrol('Style','text','String','Starting Job...','Position',[la,Top-z20*(H20+5)-z30*(H30+5),80,H20]);
z30=z30+1;
movegui(f,'center')
f.Visible='on';
drawnow;
c = parcluster();
warning('off');
job = batch(c,@Logger,0,{},'Pool',1);
warning('on');
a=job.StartDateTime;
while isempty(a)
a=job.StartDateTime;
end
LogButton.String='Stop Logging';
%% Callback Functions Start
function Test_Button(~,~)
while TestButton.Value
TempUp.String=round(rand*10);
Value=TempUp.String;
Value2=TempUp2.String;
save('G:\Users\kwilkin2\Documents\MATLAB\Data Analysis App\data.mat','Value','Value2')
drawnow;
pause(0.25)
end
end
function Test_Button2(~,~)
while TestButton2.Value
TempUp2.String=round(rand*10);
Value=TempUp.String;
Value2=TempUp2.String;
save('G:\Users\kwilkin2\Documents\MATLAB\Data Analysis App\data.mat','Value','Value2')
drawnow;
pause(0.25)
end
end
function Logger(source,eventdata)
fileID = fopen('G:\Users\kwilkin2\Documents\MATLAB\Data Analysis App\log.txt','a');
c=clock;
mm1=c(5);
mm2=mm1;
k=0;
while mm2 < mm1+2
k=k+1;
c=clock;
hh=c(4);
mm2=c(5);
ss=c(6);
BatchDisplay.String=['Iteration Number' k];
drawnow;
load('G:\Users\kwilkin2\Documents\MATLAB\Data Analysis App\data.mat','Value','Value2')
fprintf(fileID,'The current time is %2i:%2i:%4.1f and the values are 1=%s & 2=%s\n',hh,mm2,ss,Value,Value2);
pause(0.25);
end
fclose(fileID);
end
function Log_Button(source,eventdata)
if LogButton.Value
cancel(job)
BatchDisplay.String='Logging Off';
LogButton.String='Start Logging';
else
LogButton.String='Starting Job...';
drawnow;
warning('off');
job = batch(c,@Logger,0,{source,eventdata},'Pool',1);
LogButton.Enable='off';
warning('off');
a=job.StartDateTime;
while isempty(a)
a=job.StartDateTime;
end
LogButton.String='Stop Logging';
LogButton.Enable='on';
end
end
end

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!