Can a string be converted into a function call inside a GUI?

3 次查看(过去 30 天)
I am trying to call upon a matlab GUI inside another matlab GUI. This matlab GUI has the name of a string which is available inside the GUI. I want to know how to change this string into a function, so that it can be called upon to open a GUI.
Thus my question is; how can I convert a string into a function so that it can call upon another GUI in matlab?
I am trying to build a GUI in matlab app designer. The idea is that the GUI startup function calls for a customized function called AutomaticUpdate. This function searches a specific shared directory for the newest version of itself. (e.g. GUI_v1_1 look whether a GUI_v1_2 or higher is present inside the set directory). When found, the AutomaticUpdate function copies this new version from the shared directory to the directory from which it is run. I now want GUI_v1_1 to automatically start GUI_v1_2 after it has been copied.
The process of copying the GUI from the shared directory to the one from which the 'old' GUI is ran is based on the comparison of the numbers present in the GUI titles by turning the titles into strings and then selecting and comparing the numbers in this string. The newest version available in the shared directory can be selected and copied this way. I now have the title of the GUI in string format, but to open it in the GUI, I need to have it in function format. Copying the GUI name into the GUI startup function manualy after it has been copied (i.e. GUI_v1_2) works and starts the second GUI. Taking the string directly after the GUI has been copied and applying str2func to that string does not.
If the GUI that has just been copied from the shared folder was named TestApp_v1_2 , This works: (ynupdate returns 1 or 0 depending on whether a file was copied from the shared directory).
function startupFcn(app)
if haveInet
[ynupdate, fname] = AutomaticUpdate2();
if ynupdate == 1
TestApp_v1_2
end
end
end
This does not:
function startupFcn(app)
if haveInet
[ynupdate, fname] = AutomaticUpdate2();
if ynupdate == 1
str2func(fname)
end
end
end
seeing as the newest file copied is named TestApp_v1_2, fname contains the string 'TestApp_v1_2', so I would expect this to work. I need this to work because the code has to be independent of the name of the version that has just been copied. It should always open the newest version that was just copied.
  2 个评论
Stephen23
Stephen23 2019-4-25
编辑:Stephen23 2019-4-25
Your code has a major bug. The code that you wrote
str2func(fname)
calls str2func, which returns a function handle, exactly as its documentation states. However you then totally ignore this function handle (i.e. you do not allocate it to any variable, as its documentation clearly shows) and so you simply discard it without doing anything with it. And so, in contrast to your description, you did not actually call any function (Note that nowhere in the str2func documentation does it state that str2func will call a function).
The str2func documentation shows how to use it, including working examples:
You should also learn about function handles:
Loek Meijers
Loek Meijers 2019-4-25
You're right. The problem was indeed that I did not understand what str2func did, and what a function handle is. I do now, thanks a lot!

请先登录,再进行评论。

采纳的回答

Rik
Rik 2019-4-25
While you could do it like this, I personally replace the contents of the file itself on update and ask the user to restart the function. For an example of this implementation, have a look here.
I'm not sure if it works (due to JIT/runtime compilation), but you may be able to use your strategy by using eval. I'm usually opposed to the use of eval, but I suspect this is one of the few times where it is a viable option. Of course you need to make sure that your updater function always returns a valid function name.
  5 个评论
Rik
Rik 2019-4-25
Isn't the JIT a moot point? An update is probably not going to happen so often that JIT optimization will have a large impact. I expect the timing difference caused by your internet connection is likely to be bigger than any JIT difference.
Would there be other advantages? Carefully reading the doc it doesn't look to me like there are any other differences in this specific case (unless there is a variable with the exact name that the updater returns, which there isn't). In the extremely unlikely event that there is an internal function with that name, there would be a difference.
Bottom line: there shouldn't really be a difference, but in edge cases str2fun should be a slightly better choice than eval.
Guillaume
Guillaume 2019-4-25
编辑:Guillaume 2019-4-25
In this case, indeed there's probably not much difference between eval or str2func. However, as a reviewer if I see eval in a code, there's all sorts of alarm bells going off that don't get triggered as much by an str2func. At least, I know that fname is not an expression that's going to create new variables or overwrite existing ones, or do more nefarious things.
Given the choice between eval and str2func, use the one that has the least potential to do the unexpected.

请先登录,再进行评论。

更多回答(1 个)

Guillaume
Guillaume 2019-4-25
"This does not"
str2func(fname)
Yes, because str2func doesn't invoke the function, it just returns a handle to the function. You still have to call the function using the handle, so:
guifun = str2func(fname); %get function handle.
guifun(); %call function handle.
However, in my opinion, you'd be better off embedding the version number in some other way (comment in the code?, get it from the version control system? Text in a separate file?) and using a fixed name for your GUI.

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by