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.

采纳的回答

Geoff Hayes
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?
  1 个评论
Maarten
Maarten 2014-10-3
That is what I found out already, but thanks for the answer. I looked it up and found out that it is not possible to save the parameters to a .m-file like I thought it could. Currently I am using this code:
function CalcTbrkth_Callback(hObject, eventdata, handles)
%Calculate Tbrkth function
% Define variables
Tbrk = str2double(get(handles.Tbrk,'String'));
Tc = str2double(get(handles.Tc,'String'));
f = str2double(get(handles.f,'String'));
cv = str2double(get(handles.cv,'String'));
Wth = str2double(get(handles.Wrel,'String'));
%Calculate Tbrkth and set answer to static text box Tbrkth
Tbrkth = f * Wth + Tc + (Tbrk - Tc) * exp(-cv * Wth);
set(handles.Tbrkth,'String',Tbrkth);
%Save Tbrkth and other variables to workspace
assignin('base', 'Tbrkth', Tbrkth);
save('parameters.mat','Tbrk','Tc','cv','f','Wth','Tbrkth');
Thanks for the answer. I am now trying to find out how I can generate a "comma" error, when a value with a comma is typed in.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Event Functions 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by