Launch a matlab script when push button app designer Matlab
Show older comments
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
Koussou Axel
on 30 Apr 2020
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
Lucas S
on 30 Apr 2020
Raed Wasseghi
on 10 Oct 2020
Edited: Raed Wasseghi
on 10 Oct 2020
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?
Answers (1)
- Open your project in app designer : https://www.mathworks.com/help/matlab/ref/appdesigner.html
- Add the button: https://www.mathworks.com/help/matlab/creating_guis/choose-components-for-your-app-designer-app.html
- 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
- 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.
- Now you can call your external function with the file path input.
General tutorial video: https://www.mathworks.com/videos/app-designer-117921.html
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
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.
Lucas S
on 12 Apr 2019
Lucas S
on 12 Apr 2019
Adam Danz
on 12 Apr 2019
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.
Lucas S
on 12 Apr 2019
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!
Lucas S
on 12 Apr 2019
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.
Categories
Find more on Desktop in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!