Updating variable from another place in the code

6 visualizaciones (últimos 30 días)
Ricardo Burbano Escolà
Ricardo Burbano Escolà el 13 de Jun. de 2023
Editada: Harsh Mahalwar el 19 de Jun. de 2023
I know the phrasing of the question is a bit poor, but I didn't know how to explain it better. To sum up the code down below, this function starts and stops the recording mode of an external DAQ. It starts decoding the messages once I press the "Start" button, which then changes to "Stop", and I want to stop decoding the messages that I get from the DAQ once I press "Stop". Hence the recordingActive variable, which will make the code exit the while loop in the if condition. The problem is that I need to update the value of recordingActive in the elsif condition, because that's what is run when pressing "Stop".
I tried using the drawnow function, which in the documentation says it updates callbacks, but I didn't obtain any results. Any ideas on how could I solve this issue?
function StartRecordingButtonPushed(app, event)
if app.StartRecordingButton.Text == "Start Recording"
% Change the text in the button
app.StartRecordingButton.Text = "Stop Recording";
% Determine the total number of active input channels
numberOfActiveChannels = 0;
for i = 1:length(app.newChannelSetup.channels)
if app.newChannelSetup.channels(i).enabled == true
numberOfActiveChannels = numberOfActiveChannels + 1;
end
end
% Get the TCP destination port
recorderPort = GetRecorderDestinationSocketPort(app.ip, app.timeOut);
% Start recording
RecorderMeasure(app.ip, app.timeOut);
% Open the TCP stream
app.binaryStream = tcpclient(app.ip, recorderPort.tcpPort);
recordingActive = true;
bufferDraining = true;
% Read and decode the messages
while bufferDraining == true
for i = 1:numberOfActiveChannels
[app.interpretationMessages,app.signalDataMessages] = MasterMessageDecoder(app.binaryStream, app.interpretationMessages, app.signalDataMessages);
end
if recordingActive == false
if toc >= 0.5
bufferDraining = false;
end
end
end
% Stop recording
RecorderStop(app.ip, app.timeOut);
% Clear the buffer
clear app.binaryStream;
elseif app.StartRecordingButton.Text == "Stop Recording"
% Start calculating time to stop draining the buffer
tic
% Change the text in the button
app.StartRecordingButton.Text = "Start Recording";
recordingActive = false;
end
end

Respuesta aceptada

Harsh Mahalwar
Harsh Mahalwar el 19 de Jun. de 2023
Editada: Harsh Mahalwar el 19 de Jun. de 2023
Hi Ricardo,
I understand, you want to update a variable from a different location and want the change in the variable to impact a running process. Also, it looks like you’re using the app builder.
To change the variable and have it’s changed value impact a running process without passing it’s changed value in a function’s parameters. You can add the variable as a property. Then you can change the value of that property with any function and still use the changed value of that variable in an another function without passing it as a parameter.
Code example:
app.property_name = [new value];
drawnow
In the above example, I am changing the value of the “property_name” to “new_value”. As “property_name” is a property of the app builder’s class, it’s value will be consistent whenever changed.
Also, don't forget using the drawnow function to immediatly change the value of the variable.
You can use the above link to learn more on how you can use app properties and objects.
Thanks and regards
Harsh Mahalwar

Más respuestas (0)

Categorías

Más información sobre Act on Data en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by