Change, save and load .m-file parameters through GUI
2 次查看(过去 30 天)
显示 更早的评论
Hi there,
I've got a new question here. I am working on a GUI for a simulink model, which is now controlled by an .m-file called parameters.m. At te moment I fill in the variables in the .m-file, run this m-file and then run the simulation in Simulink.
I want to set the variables in the .m-file through my GUI, see the figure below:
I set the edit text to values. These values should be saved to the parameters.m-file. When "Calculate Tbrkth" is pressed, the parameters.m-file runs and calculates Tbrkth ( NOTE: this is another .m-file than that from the GUI! ). This value is the output of the parameters.m-file and should be shown in the GUI.
My problem is that the variable values won't be saved to the parameters.m-file. I programmed the code below in the .m-file of the GUI:
function Tbrk_Callback(hObject, eventdata, handles)
%Define Tbrk to get value from edit text box
Tbrk = get(handles.Tbrk,'String');
%Assign Tbrk to workspace
assignin('base', 'Tbrk', Tbrk);
%Save Tbrk to m-file
save('parameters.m','Tbrk');
This is my first problem I ran into. After this problem solved, I can work out my GUI further and eventually bump into new problems.
0 个评论
采纳的回答
Geoff Hayes
2014-10-2
Maarten - is the function Tbrk_Callback a callback for the text field Tbrk, or is it for the push button? If it is the former, then you may want to reconsider putting this functionality in the push button callback - let it grab all data from the five fields, save them to the parameters file, and then run the code. Something like the following (note that some of the tags/names of the widgets will be different from yours, but the idea is the same)
function calcTbkrthPushbutton_Callback(hObject, eventdata, handles)
% get the data from the five widgets via the handles object
Tbrk = str2num(get(handles.Tbrk,'String'));
Tc = str2num(get(handles.Tc,'String'));
f = str2num(get(handles.f,'String'));
cv = str2num(get(handles.cv,'String'));
Wrel = str2num(get(handles.Wrel,'String'));
% save the parameters to file
save('parameters.mat','Tbrk','Tc','f','cv','Wrel');
% do other stuff
So the above will save the five parameters to a mat file which may not be what you intended since you say that you run this m-file, and you can't "run" a file of this type (the MATLAB binary formatted file). You can load the parameters from this file. Is that what you meant?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Event Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!