Why is my script not detecting changes in UI?

2 visualizzazioni (ultimi 30 giorni)
Sasha
Sasha il 16 Mag 2024
Commentato: Sasha il 17 Mag 2024
Hi all! I am relatively new to app design in Matlab and I'm trying to design an app for an automated recording set-up. The idea is that users can define metadata in the UI (animal ID, duration of trial, etc.) before starting the session, then start and pause the session as needed, and then monitor the recording from the app while the automation is controlled in a separate matlab script (i.e. the automation is not running on the callbacks). I can get my app to pass information back to the base script using assignin for basic inputs and pass data back to the UI for monitoring like a timer and temperature. However, I can't figure out how to execute my code only when the user starts the trial. To start with something simple, I have written an UpdateTimer function that updates the timer when called in the main script (I had originally written a timer into the callback but then nothing else can run so now the user will see the timer update everytime something runs in the automation to approximate the time since starting) and I have some dummy temperature data. When the user clicks Start on the UI, I would like the timer and temperature to update every 10 seconds. However, no matter what I try, the script can't tell when the button has changed. Can anyone help me??
Main Script:
clear all;
close all;
b1_State = 'NotStarted'; %Initial state of start button
Box1 = TestApp; %Calling UI
%pause(15); %I can pause is for 15seconds and quickly enter everything and then hit start and the following will work, but I don't want the user to be on a time limit before starting the session
switch b1_State %This was the last thing I tried, I've also tried while and if loops with no success either
case 'Recording'
test = 5;
Box1.Timer_EditField.Value = UpdateTimer(b1_StartTime, b1_PauseTime, b1_Duration, Box1);
Box1.TemperatureCEditField.Value = num2str(25);
pause(10)
Box1.Timer_EditField.Value = UpdateTimer(b1_StartTime, b1_PauseTime, b1_Duration, Box1);
Box1.TemperatureCEditField.Value = num2str(35);
end % Eventually I will specify what happens when the script is paused, but I just wanted to get one case working first
UpdateTimer Script (Note I'm still working on what happens if the user pauses and then continues the recording):
function update = UpdateTimer(StartTime, PauseTime, Duration, Box)
Now = datestr(now, "HH:MM:SS.FFF");
Now_H = str2double(Now(1:2));
Now_M = str2double(Now(4:5));
Now_S = str2double(Now(7:12)) + Now_M*60 + Now_H*3600;
Start_H = str2double(StartTime(1:2));
Start_M = str2double(StartTime(4:5));
Start_S = str2double(StartTime(7:12)) + Start_M*60 + Start_H*3600;
Update_S = Now_S - Start_S;
if Update_S < Duration*60
A(1) = floor(Update_S/3600);
A(2) = floor((Update_S - A(1)*3600)/60);
A(3) = Update_S - A(1)*3600 - A(2)*60;
update = sprintf('%02.0f:%02.0f:%02.3f', A(1), A(2), A(3));
else
Box.Timer_EditField.Value = 'Session Over';
Box.PAUSEButton.Text = 'END';
Box.PAUSEButton.BackgroundColor = 'Red';
Box.LampColor = '#9aa0a2';
Box.STARTButton.Text = 'START';
end
UI Script Attached if needed!
Thank you SO much in advance for any guidance!!
  2 Commenti
Stephen23
Stephen23 il 16 Mag 2024
Modificato: Stephen23 il 16 Mag 2024
" However, I can't figure out how to execute my code only when the user starts the trial.... the script can't tell when the button has changed. Can anyone help me??"
You are still writing linear code: first A executes, then B, then C, etc. until your script reaches the end.
But GUIs are fundamentally asynchronous code, and thinking in terms of linear code is a major hinderance to writing a GUI (i.e. you need to change how you think about code being executed). Note that in general GUI events will occur in no particular sequence. It takes a while to change how you think about code, but here are a few tips:
  • write functions (not scripts).
  • the GUI calls your functions (not a script calls a GUI)
  • in other words: callback functions are triggered by GUI events
  • share data properly within the GUI (i.e. avoid evil ASSIGNIN et al).
Take a look on FEX to find plenty of GUI examples which show how to perform basic actions (like reading a button status), they are often a good way to see how tasks should be achieved:
Sasha
Sasha il 17 Mag 2024
Thank you Stephen, this helps for sure!! I'm working off of old scripts and can't ask the original author why and how it all worked together, so this helps clear up how they were communicating! Cheers!

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Develop Apps Using App Designer in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by