Can a string be converted into a function call inside a GUI?
Mostrar comentarios más antiguos
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 comentarios
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
el 25 de Abr. de 2019
Respuesta aceptada
Más respuestas (1)
Guillaume
el 25 de Abr. de 2019
"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.
Categorías
Más información sobre Functions en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!