Loading .mat file from within GUI not doing anything?!

8 次查看(过去 30 天)
Hi,
I have made a simple concept GUI to test populating a drop down menu based on the .mat files in a directory, and then loading the selected .mat file.
My code (attached - change the directory to where you save it...) is notdoing anything when I try to load the .mat file.
On the other hand if I type in the command window:
handles.Selection = 'Data2.mat'
load(handles.Selection)
It loads fine... Why is this? I'm lost!
Thanks,
Matt
  5 个评论
Matt
Matt 2016-9-6
That seems to be the case. Thanks.
This hadn't crossed my mind as I thought I was loading the .mat into the base workspace.
How can I prevent the loaded data being cleared from the work space when the function has completed?
Sorry for such basic questions, but I am finding this area difficult, despite having written two 10+ interlinked GUI window programs!
Henry Giddens
Henry Giddens 2016-9-6
编辑:Henry Giddens 2016-9-6
Hi Matt, I'm going to try and attempt to explain this, but it may not be very clear...
Think of Matlab as having a list of functions that need to be executed. Your GUI is a simple function that has to run. When a GUIDE GUI is launched it runs through the opening function and then the output function and then the list of functions left for matlab to execute is empty. Control is handed back to the command window.
Now, to get an output from a gui where the user has interacted to influence what that output is, the uiwait(hObject) command is used to halt any further execution of matlab's list of functions, until the gui is closed (or uiresume is called). The outputfcn in the gui '.m' file can be modified so that when it executes, an output is returned from the gui function, that depends on how the user used that gui.
Take a look at some of matlabs inbuilt gui functions to distinguish between gui functions that return an output based on how the user interacted with the gui, and those that don't.
For examples of the first:
uigetfile
menu
examples of the second
errordlg
If (and I'm guessing here) you want to create a GUI that populates the base workspace every time you click the load button, then this isn't really possible. You would have to close the gui once the file has been selected*, outputting the relevant data via the output_fcn (or in this case it would be more sensible to output the path to the file, and load the data afterwards)
Maybe take a look at this video for getting outputs from guide GUIs (I haven't actually watched it but i imagine it explains how it works). http://blogs.mathworks.com/videos/2010/02/12/advanced-getting-an-output-from-a-guide-gui/
*You dont actually have to close the gui, only to call uiresume, for the output_fcn to execute. However, leaving the gui open would be a bit misleading/pointless

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2016-9-6
编辑:Stephen23 2016-9-6
There is no such thing as " the workspace"... you might be thinking of the base workspace when you say that, but in fact every function and callback has its own workspace. And load is correctly loading into whichever workspace it is called from.
If you want to understand what MATLAB is doing, and how to pass your data between workspaces, then it is time do some reading:
TIP: avoid globals, assignin, evalin, etc.
  2 个评论
Matt
Matt 2016-9-6
编辑:Matt 2016-9-6
Thanks - that clarifies the way workspaces are handled.
I have read the links, but I am still unsure what the best course of action is to be honest.
Stephen23
Stephen23 2016-9-6
编辑:Stephen23 2016-9-6
Lots of MATLAB users use guidata to pass data between callbacks. The internet has thousands of examples.
Personally I like using nested functions, which are a very neat and convenient way of passing data between callbacks and other functions. Here is an example (first save a simple mat file with the name 'test.mat', containing one variable test):
function main()
mat = [];
uicontrol('Callback',@sub)
%
function sub(~,~)
% Always assign load to a variable:
S = load('test.mat');
mat = S.test;
show()
end
%
function show()
disp(mat)
end
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by