Pass GUI data to another .m file/function and from that to workspace
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
D Valavan
am 14 Dez. 2017
Kommentiert: D Valavan
am 15 Dez. 2017
Hello. I am trying to make a GUI using GUIDE. For starters it will have a popupmenu and a pushbutton.
Things I have changed in the default-generated code:
In popupmenu1_CreateFcn to only show specific file extensions:
d = [dir('*.txt'); dir('*asc.dat')];
set(hObject, 'String', {d.name});
and in pushbutton1_Callback to get the filename:
handles.answer = get(handles.popupmenu1, 'Value');
I tried to write the filename into the figure's 'answer' path, I don't know if that is correct.
I then want to get that filename and enter it into another function in another .m file, that uses it like this:
function y = importfiledat(filename, startRow, endRow)
...
fileID = fopen(filename,'rt');
...
y = ...
The importfiledat function works fine when called by itself from the workspace and saves y as a variable there.
As it stands, running the GUI function shows correctly the popupmenu and pushbutton, but pressing the button results in various errors, an 'fopen error' between them. I tried calling the importfiledat function from the pushbutton1_Callback function or using assignin, evalin and whatever else I found, with no success.
Please advise as to how I can pass the information correctly.
Also, if I then add a plot in the GUI, will I have any problem getting an already drawn plot from the workspace? The position and characteristics of the GUI objects are changed from the .fig file? Shall I move the popupmenu dir text to that file somehow?
Is the more basic(?) functionality of normal programming way newbie-friendlier than GUIDE?
Thank you, D.
1 Kommentar
Jan
am 14 Dez. 2017
It is not clear what "getting an already drawn plot from the workspace" means. What exactly is "a drawn plot"? Figures, axes and line objects created by plot are not part of "the" workspace. Any function has its own workspace, which is the set of locally used variables, but the diagrams are not variables.
Akzeptierte Antwort
Jan
am 14 Dez. 2017
Bearbeitet: Jan
am 14 Dez. 2017
Search in the forum for "share variables between callbacks".
handles.answer = get(handles.popupmenu1, 'Value');
stores the current value of the popup menu only, but not its contents. I assume you need:
value = get(handles.popupmenu1, 'Value');
str = get(handles.popupmenu1, 'String');
handles.filename = str{value};
guidata(hObject, handles); % Important! Store modified handles struct
Then you can access the filename in another callback:
y = importfiledat(handles.filename, startRow, endRow)
Using assignin and evalin to poke the value of filename as a variable to the base workspace is a bad idea. As you see, it is not easy, but such remote creation of variables is hard to debug also. Better use the variables create in a GUI inside this GUI only. Start the calculations from a callback of this GUI. The magic transfer of the variables to the command window is a confusing indirection only.
Then if you get the output y of the calculation, you have it where you need it to create some diagrams inside the dialog directly. This is much better than creating the graphics somewhere else and try to move them around.
I do not work with GUIDE, but create all GUIs by code. This has less limitations with the compatibility between Matlab versions. But I do not think that it is easier for a beginner. See https://www.mathworks.com/matlabcentral/fileexchange/24861-41-complete-gui-examples .
3 Kommentare
Jan
am 15 Dez. 2017
Using scripts and the base workspace (the set of variables known in the command window) works until a certain level of complexity. The reproduction of results gets harder, when the program grows.
Using functions, which contain the complete workflow, improve the quality of the software and reproducibility of the results. In addition the code is easier to debug.
I do not know, why "nothing" happens, when you press the button. But you can find this out easily: Set a debugger breakpoint in the concerned callback. Then you can step through the code line by line and see directly, what happens. See https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!