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 view (last 30 days)
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);

Accepted Answer

Dennie
Dennie on 16 Oct 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 Comments
Dennie
Dennie on 19 Oct 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
Edited: Mohammad Abu Bakar Siddique on 20 Oct 2015
HI Thanks a lot again i use this code and it works finely.and one more thing how could I update a edit text value in simulink model after pushing a pushbutton??? i can update this in manually but is it possible to do with a pushbutton ....after when i push a push button then edit text value autometically updated to the simulink model.....thanks in advance
function edit_topo_Callback(hObject, eventdata, handles)
set(0,'ShowHiddenHandles','on'); handles=guidata(hObject); val=get(hObject,'string'); %put the value to text set(handles.edit_topo,'string',num2str(val));
%Update SIM model set_param('DCDCconverter/Constant','value',num2str(val));
guidata(hObject,handles);
% --- Executes on button press in pushbutton_select. function pushbutton_select_Callback(hObject, eventdata, handles) % hObject handle to pushbutton_select (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
handles=guidata(hObject);
guidata(hObject,handles);
here is the code what i use

Sign in to comment.

More Answers (0)

Categories

Find more on Modeling in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!