Launch a matlab script when push button app designer Matlab

Hello !
I have already created a button that allow user to browse a file and a TextArea that display the File Path.
function Browse(app, event)
[FileName,FilePath ]= uigetfile();
ExPath = fullfile(FilePath, FileName);
app.FileTextArea.Value = ExPath;
end
And now i would like to create another button that launch a matlab script (function) with the file in parameter I don't know how to do that, first time i use app designer.
Thanks for helping !
Edit : i tried this :
function Launch(app, event)
Path = string(app.FileTextArea.Value);
testLectureXML(Path);
end
testLectureXML is my matlab script.
But it returned me :
Undefined function 'Launch' for input arguments of type 'Apptest'.

3 Comments

Hello !
I'm facing the same problem.
Have you managed to launch your matlab script, with your file loaded as a parameter, after pushing your button app?
Thanks
Hello !
Yes. I put an Edit Text next to the Browse button to display the name of the file.
And then in the callbackFunction of the button ("play" or "run" in don't know what is your button app) you just create a variable which contains the value of the EditText and then you just call your script with the variable inside.
function RunButtonPushed(app, event)
file = app.EditText.Value;
scriptName(file);
end
hi!
I am having the same problem but the solution you have posted somehow doesnt work for me.
In my app designer there's no "edittext" but rather an "edit field (text)" and "editfield (nummeric)" and if I execute this code:
function RunButtonPushed(app, event)
file = app.Editfield.Value;
scriptName(file);
end
and i get this error: Attempt to execute SCRIPT Test_Zeilentausch_qualitaet as a function: [path]
But I can't write "app.editfield.string" it doesnt exist.
Do you have any solution on this? and do you know if the values appear in the workspace once I run the script?

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 12 Apr 2019
Edited: Adam Danz on 12 Apr 2019
  1. Open your project in app designer : https://www.mathworks.com/help/matlab/ref/appdesigner.html
  2. Add the button: https://www.mathworks.com/help/matlab/creating_guis/choose-components-for-your-app-designer-app.html
  3. Add a ButtonPushedFcn callback function to the button: https://www.mathworks.com/help/matlab/creating_guis/write-callbacks-for-gui-in-app-designer.html and https://www.mathworks.com/help/matlab/ref/matlab.ui.control.button-properties.html#d117e1549488
  4. From within the callback function, use the "app." structure to find the handle to your GUI object that contains the file path string. Then access the string property.
  5. Now you can call your external function with the file path input.
Don't get intimidated. Dive right in and if you get stuck, feel free to ask follow-up questions below as a comment under this answer (share what you tried and why it's not working).

9 Comments

Thanks ! I will try with the tutorial !
What is "Launch"? Is that a function you manually added or is that the name of the ButtonPushFcn callback function? Follow the first link in step #3 to add the callback function. That will generate the function in your code and all you need to do is add content to the function. I suggest you keep the function name that is generated rather than changing the function name.
Edit : i tried this :
function Launch(app, event)
Path = string(app.FileTextArea.Value);
run testLectureXML;
end
The fact is i want to run the function with the textarea Value in parameter. Here i run the script how can i give a parameter to the function in the script ?
Launch is the function of my Button "Launch" that have to run the matlab function of a script .m with app.TextArea.Value in parameter
app.FileTextArea <-- is that the handle to the object that stores the file path string? What is that object? Is it a text box and is the file string the only thing stored in that text box? If my assuptions are correct, you should access the path string like this:
app.FileTextArea.String
If Launch(app,event) is the ButtonPushedFcn(), when you press that button it will call Lunch().
function Launch(app, event)
testLectureXML(app.FileTextArea.String);
end
But this assumes that your path is a string stored in app.FileTextArea.
Capture.PNG
Maybe it's better if you have a picture :
Browse is to search a file in computer's files
The "Text Area" component "File :" is empty at the start. When you open the file , the path of the file is written in the Text Area.
and then the Launch Button is what i try to do :
Run the
function testLectureXML(file)
(from my file testLectureXML.m) with the file Path in the Text Area as parameter
But when i do run testLectureXML(Path) (Path is the value of the textarea) it says me that Path.m is not exist or something like that.
Ok, this definitely helps.
Prior to your ...xml() function, use the A = exist(filepath, 'file') function to determine if the file exists (check out the link). Assuming the file path is complete and accurate, if the file doesn't exist you might just need to add it to the matlab path using addpath(fileparts(filepath)).
If the error continues, please share the entire copy-pasted error message along with the full file path string you're passing to your funciton. You're 90% there!
You mean i have to use exist() in my .m function or in my app code ?
You could do either (or both). If your .m function is designed to be called by additional callers other than your app, it would be smart to do that in your external file.
Doing it in your app checks the validity before sending the variable. It's more of a conceptual question. I'd do it in the external file.

Sign in to comment.

Categories

Asked:

on 12 Apr 2019

Edited:

on 10 Oct 2020

Community Treasure Hunt

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

Start Hunting!