GUI help with loading .mat file

4 Ansichten (letzte 30 Tage)
scurvy
scurvy am 2 Nov. 2015
Beantwortet: Geoff Hayes am 3 Nov. 2015
I'm very new to coding and matlab and have put this code together through watching a few youtube videos. basically my issue is with my GUI code at the very end there is a function labeled 'onFileOpen' in the menu that was created. this function calls back to nothing and i'm not sure how to set it up so that when i'm in the GUI i click to open a .mat file that populates the GUI with all of my info and calculates the solutions. the instructions i followed said to first load the info into my work space by typing in the name of my first matlab code which works at populating the work space. it mentions to close all other windows which i have done but when i go in the guy to open my file i get an error as follows "Undefined function or variable 'onFileOpen'.
Error while evaluating Menu Callback"
i think i'm aware of what the issue is but i don't know how to solve it. below is the menu bit of my GUI code
hMainMenu=uimenu('Label','&File');
hOpenMenu=uimenu(hMainMenu,'Label','Open...','CallBack','onFileOpen');
uimenu(hMainMenu,'Label','Exit','CallBack','onFileExit');
if successful the GUI should load an image of the truss system as well as calculate all of the variables.

Antworten (1)

Geoff Hayes
Geoff Hayes am 3 Nov. 2015
scurvy - the error message is telling you that the callback function onFileOpen (and onFileExit) cannot be found either because the file is not within the MATLAB search path or it doesn't exist (i.e. no file named onFileOpen.m). I suspect that the latter is true.
What you need to do is either create the callbacks in files named onFileOpen.m and onFileExit.m, or define the functions within your script as follows
function mainGui
hMainMenu=uimenu('Label','&File');
hOpenMenu=uimenu(hMainMenu,'Label','Open...','CallBack',@onFileOpen);
uimenu(hMainMenu,'Label','Exit','CallBack',@onFileExit);
function onFileOpen(hObject, eventdata)
fprintf('onFileOpen called\n');
end
function onFileExit(hObject, eventdata)
fprintf('onFileExit called\n');
end
end
Note how the callbacks are nested within the main (parent) GUI function. See nested function for a description of thee types of functions and their benefits (notably the sharing of variables between the parent and nested function).
An alternative to the above, which could be easier for a new user of MATLAB, is to use GUIDE to create your GUI. See creating a simple GUI with GUIDE for one such example.

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by