Loop only the first execution
显示 更早的评论
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
回答(1 个)
Walter Roberson
2016-11-17
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 个评论
Me29
2016-11-17
Walter Roberson
2016-11-17
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
类别
在 帮助中心 和 File Exchange 中查找有关 TCP/IP Communication 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!