stringmatlabmatlab-guimatlab-app-designer

Is there a way a string be converted into a function call inside a matlab GUI?


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.

This works (if the GUI that has just been copied from the shared folder was named TestApp_v1_2):

   function startupFcn(app)
       if haveInet
           fname = AutomaticUpdate2();
       end
   AppTest_v1_2
   end

This does not:

   function startupFcn(app)
       if haveInet
           fname = AutomaticUpdate2();
       end
   str2func(fname)
   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.


Solution

  • use str2func to build a function handle (instead of eval)

    You are half wrong when you say "it does not work" in your second code snippet. It is just that you haven't finished calling the function:

    str2func(fname)
    

    by itself does nothing visible indeed. It only create a handle to a function fname, but since you call the line without any output argument the created handle is lost as soon as the line finishes executing.

    Now if you try:

    myNewUpdatedApp = str2func(fname) ;
    

    Then myNewUpdatedApp is now pointing to an executable function, but you still need to actually call it to start it executing. So to do the job completely, use:

    myNewUpdatedApp = str2func(fname) ; % create the function handle (pointer to funtion)
    myNewUpdatedApp ;                   % call the execution of the function
    

    I encourage you to read about: