You are loading mat file inside the callback function. Calling load() inside a function is unrecommended. Please refer to the link and below comments to get an idea of why is this the case. It might be easy to load variable in base workspace and then pass it to both callbacks as 4th argument.
how to use a (matrix) variable from one callback to another callback in GUI?
댓글 수: 6
I agree with you that I should have provided a reference to the resources supporting the statement, which I have added now. But still, there is one confusion. Although you added the loaded matrix to the handle in your first callback, what will happen when the first callback is executed again and the following line is executed
S = load('PMelisa01.mat', 'PMelisaD20180404Walkingnone01');
It appears that MATLAB will again load the file, or is there something going on.
It makes sense that for custom GUI, there is no need to load variable in the base workspace. But instead of loading it in a repeating callback function, it might be better to load it in createFcn of the graphics object and assign add it to the handle. That way, repeated calls to load() can be avoided.
"It appears that MATLAB will again load the file..."
This is correct: the file will get loaded every time that callback runs. This is simply because of how that GUI has been designed by its author: it may be that that is exactly what the author intended... or maybe not. I have no idea, because the design requirements were not given in the question. Perhaps the data file is changed by an external program, and the GUI should load it again each time. I have no idea, because we were not told anything about this.
As you point out there is no need for the data to be reloaded each time: it would be quite easy to provide a button that lets the user load the data whenever they wanted to, or to add some conditions that check if the data has already been loaded (e.g. check if handles.data exists, or the size, or with a flag), or to load the data once automatically at the start... however these are design considerations for the GUI's author: things to be decided based on what it needs to do and how it should interact with the user and data. It is not my job to decide that.
"it might be better to load it in createFcn of the graphics object and assign add it to the handle"
Personally I am not a big fan of things happening automatically like that (particularly when things go wrong, as they inevitably will...), but that would certainly work.
채택된 답변
- callback 1: add the data as a field of the handles structure.
- callback 1: at the very end of the callback save handles using guidata.
- callback 2: the handles structure contains your data as field.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!