Adding a Callback to a Button to import data onto a plot

With the new version of MATLAB GUI, the structure has changed. I have added a callback to a button
% Button pushed function: Button
function ButtonPushed(app, event)
I cannot change the above^.
I have used the uigetfile function to import data.
[filename, pathname] = uigetfile('*.*');
plot('U',app.UIAxes);
Now I'm wondering how to import it onto app.UIAxes, can anyone help with this? I'm assuming my second line of code is wrong.
Thank you

 Accepted Answer

Cris LaPierre
Cris LaPierre on 31 Mar 2021
Edited: Cris LaPierre on 31 Mar 2021
You import it to the workspace, not the axis. Once you've created variables with your data, say X and Y, then plotting is as simple as plot(app.UIAxes,X,Y).
Your plot syntax is incorrect. Consult the documentation. The axis target is the first input. Also, what is 'U'? You can't plot a character.
If your code is working correctly in MATLAB, the only change you should need to make is to specify the target axes in your plot command. Can you successfully import a file and plot it in MATLAB?

4 Comments

Thanks that's super helpful. U is a variable like X,Y. Is there information on how to import files using the uigetfile function for the GUI.
uigetfile is a just a graphic interface for selecting a file. It has not import capabilities. You would need to use the information returned by uigetfile to construct a method for loading/importing the selected file. You may find the fullfile function useful for this.
See this page for functions to consider for loading the file (organized by file type).
Consider going through MATLAB Onramp to learn some of the fundamentals of MATLAB. Chs 8,9, and 11 may be helfpul here.

Sign in to comment.

More Answers (0)

Categories

Find more on App Building 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!