Change, save and load .m-file parameters through GUI
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Maarten
el 2 de Oct. de 2014
Comentada: Maarten
el 3 de Oct. de 2014
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/145578/image.png)
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 comentarios
Respuesta aceptada
Geoff Hayes
el 2 de Oct. de 2014
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?
Más respuestas (0)
Ver también
Categorías
Más información sobre Simulink Functions en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!