Variable transfer from App Designer to general .m script file

163 Views Asked by At

I am trying to transfer a string (file location) from App Designer to a .m script file.

So far, in the App Designer I have:

       app.matfile = app.matfileEditField.Value;
       app.directory = app.DirectoryEditField.Value;
       app.DataSetName = app.DataSetNameEditField.Value;
       assignin('base','matfileDirectory',app.matfile);
       assignin('base','directory',app.directory);
       assignin('base','DataSetName',app.DataSetName);
       run('PostProcessMAIN.m');

Where all the app.*** are public variables. When I run the app, the .m file correctly loads the three variables. When I go into the Command Window and test, the strings are correct. I also tried cd(directory) in the Command window to check to make sure it works and it does (cd puts you in the correct file path). I get an error though when the .m file runs by itself though. I get the following error:

Unrecognized function or variable 'directory'.

Error in PostProcessMAIN (line 10)
cd(directory);

Error in run (line 91)
evalin('caller', strcat(script, ';'));

Error in PDFscanner/PostProcessData (line 116)
           run('PostProcessMAIN.m');

Error in

matlab.apps.AppBase>@(source,event)executeCallback(appdesigner.internal.service.AppManagementService.instance(),app,callback,requiresEventData,event) (line 63)
            newCallback = @(source, event)executeCallback(appdesigner.internal.service.AppManagementService.instance(), ...
 
Error while evaluating Button PrivateButtonPushedFcn.

It doesn't recognize the variable that I assigned in and can use in the Command Window for some reason. Can anyone help me please?

2

There are 2 best solutions below

0
Alexander Savadelis On BEST ANSWER

Thank you for the help! To anyone having the same problem, here is the solution:

  1. Make sure your variables are public:

    properties (Access = public) directory = ''; DataSetName = ''; matfile = '';
    end

  2. assign the app variable names from the text box in the application. Then assign in the variables to the .m file in the 'caller' workspace:

    app.matfile = app.matfileEditField.Value;
    app.directory = app.DirectoryEditField.Value;
    app.DataSetName = app.DataSetNameEditField.Value;  
    
    assignin('caller','matfileDirectory',app.matfile);
    assignin('caller','directory',app.directory);
    assignin('caller','DataSetName',app.DataSetName);
    evalin('caller','PostProcessMAIN');
    

NOTE: The .m file is PostProcessMAIN.m. However, if you put the .m in the line of script it won't recognize the file.

NOTE: Make sure you don't have 'clear' in the beginning of your .m file. This will clear all the variables you just assigned in and you will produce an error stating that the variables you are trying to use are not defined.

1
Cris Luengo On

Please, don't build applications using script M-files. Use function M-files. Your application will be a lot simpler, easier to debug, and easier to expand on.

You can make your PostProcessMAIN.m file a function file by putting at the top:

function PostProcessMAIN(matfileDirectory, directory, DataSetName)

Then in your GUI callback, instead of the code you show, do this:

app.matfile = app.matfileEditField.Value;
app.directory = app.DirectoryEditField.Value;
app.DataSetName = app.DataSetNameEditField.Value;  

PostProcessMAIN(matfileDirectory, directory, DataSetName);

(I don't know what is in PostProcessMAIN, it is possible you might need to pass in more variables.)

Every time you use assignin or evalin you're working around a design problem. Take a step back and figure out why the design requires such a workaround.

That said, there are a few proper uses of these functions, but they're few and most MATLAB users shouldn't ever run into one of these uses. But there are no uses of script M-files in larger applications. Scripts are only useful as something the user runs, they should never be invoked by other scripts or functions.