I am newbei matlab coder. i want to put a limit range in gui edit text and also want to update this value in simulink model i dit it but the value didn't update when i enter the value in edit text????? Any help is appreaciable....
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Mohammad Abu Bakar Siddique
el 16 de Oct. de 2015
Editada: Mohammad Abu Bakar Siddique
el 20 de Oct. de 2015
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','1')
warndlg('Input must be numerical');
end
x = get(hObject,'String'); % get text from handles structure
try
y = x{1}; % cell to string
z = str2num(strtok(y)); % string to number
catch
z = str2num(strtok(x)); % string to number
end
lower_limit = 1;
upper_limit = 2000;
%Output equals value of z if z falls between the bounds set by lower_limit
%and upper_limit. Otherwise it is 0 or an empty array
output = z*(z < upper_limit)*(z > lower_limit);
%If z does not fall within the bounds set by the limit parameters or is an
%empty array
if isempty(output) | (~output)
%Set the output value to the limit value closest to z
output = lower_limit*(z < lower_limit) + upper_limit*(z > upper_limit);
%If there is no value in z, set the output value to the lower limit of
%the desired bounds
if isempty(output)
output = lower_limit;
end
%Create a message string and initiate a message box informing the user
%to enter a value within the limits of the defined range
str = sprintf('Enter a number between %d and %d', lower_limit, upper_limit);
msgbox(str);
end
%Set the value of the text box to the new value, which lies within the
%value range
set(hObject,'String',{num2str(output(1))});
set(0,'ShowHiddenHandles','on');
handles=guidata(hObject);
val=get(hObject,'string');
%put the value to text
set(handles.edit_period,'string',val);
set(handles.slider_period,'value',str2double(val));
%Update SIM model
set_param('DCDCconverter/Enabled Subsystem/period','value',num2str(val));
set_param('DCDCconverter/Enabled Subsystem1/period1','value',num2str(val));
set_param('DCDCconverter/Enabled Subsystem2/period2','value',num2str(val));
set_param('DCDCconverter/Enabled Subsystem3/period','value',num2str(val));
guidata(hObject,handles);
0 comentarios
Respuesta aceptada
Dennie
el 16 de Oct. de 2015
str = get(hObject,'String');
newValue = str2double(str);
% Do the change if it's valid
if ~isnan(newValue)&& newValue>=-40 && newValue<=95
% poke the new value into the model
assignin('base','name',newValue) % assign in matlab workspace
else
% throw up an error dialog
estr = sprintf('%s is an invalid value for ... Only values allowed between -40 and 95. value is set to default (23).',str);
errordlg(estr,'Parameter Error','modal');
% reset the edit box to the old value
set(hObject,'String','23');
assignin('base','name',23);
end
This contains everything you want to know I believe. First you extract the value, then you check for validity, if it is valid you assign the value where you want ( assignin() for matlab workspace and setparam() for Simulink)
If the value is not valid, you notify the user using an error dialog and reset the value to a default.
Best regards, Dennie
5 comentarios
Dennie
el 19 de Oct. de 2015
I'm not sure what you mean. However, this is the code for enabling/disabling buttons in the GUI
set(handles.<tag_of_button>,'Enable','On') % to enable
set(handles.<tag_of_button>,'Enable','Off') % to disable
This works for basically every property of gui items
Mohammad Abu Bakar Siddique
el 20 de Oct. de 2015
Editada: Mohammad Abu Bakar Siddique
el 20 de Oct. de 2015
Más respuestas (0)
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!