Loop only the first execution
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I am trying to create a loop in a GUI that only loops the first time the program is run through, and then skip that portion any other time it comes to it. How can I do this?
This is the code I need in the loop to execute only the first time:
connection = tcpip('127.0.0.1', 3333, 'NetworkRole', 'server'); % connect to server socket via TCP/IP
fclose(connection);%Make sure socket is closed before attempting to open port
fopen(connection);
Thanks in advance for any help!
SM
0 comentarios
Respuestas (1)
Walter Roberson
el 17 de Nov. de 2016
persistent has_been_initialized
if isempty(has_been_initialized)
connection = tcpip('127.0.0.1', 3333, 'NetworkRole', 'server'); % connect to server socket via TCP/IP
fclose(connection);%Make sure socket is closed before attempting to open port
fopen(connection);
has_been_initialized = true;
end
Reminder: your variable connection looks like a local variable in the workspace of that function. If that local variable goes out of scope then the connection will be shut down.
2 comentarios
Walter Roberson
el 17 de Nov. de 2016
See the above link on the various ways you can export information from a function.
One way would just be
persistent connection
if isempty(connection)
connection = tcpip('127.0.0.1', 3333, 'NetworkRole', 'server'); % connect to server socket via TCP/IP
fclose(connection);%Make sure socket is closed before attempting to open port
fopen(connection);
end
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!