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 件のコメント
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
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 件のコメント
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 Center および File 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!