guguli
guguli

Reputation: 15

simulink run with gui matlab

i have a Gui and a Simulink Model, i wanna enter some value into the textfield in the Gui and press start Button and after this, my simulink model should take these values and run, the result should be displayed in the gui statictext.

to simulink: i have to constant blocks, thes name ist kraft and flaeche. and the oarameter into the blocks are k and f.

ok now i want to edit the values of k and f in the gui. This is my code:

      function kraft_Callback(hObject, eventdata, handles)
      kraft_value = str2num(get(hObject,'String')); 
     if (isempty(kraft_value)) 
     set(hObject,'String','0') 
     end 
      guidata(hObject, handles); 

     function flaeche_Callback(hObject, eventdata, handles)
     flaeche_value = str2num(get(hObject,'String')); 
     if (isempty(flaeche_value)) 
     set(hObject,'String','0') 
      end 
     guidata(hObject, handles);

     function start_Callback(hObject, eventdata, handles)
     k= str2double(get(hObject,'string'));
     f= str2double(get(hObject,'string'));
     sim('Steifigkeit');


    function static_CreateFcn(hObject, eventdata, handles)

But i get these Errors :

     Error using Gui>start_Callback (line 215)
      Error due to multiple causes.

    Error in gui_mainfcn (line 96)
    feval(varargin{:});

      Error in Gui (line 17)
     gui_mainfcn(gui_State, varargin{:});

        Error in                     @(hObject,eventdata)Gui('start_Callback',hObject,eventdata,guidata(hObject))


      Caused by:
     Error using Gui>start_Callback (line 215)
    Error evaluating parameter 'Value' in 'Steifigkeit/f'
    Error using Gui>start_Callback (line 215)
    Undefined function or variable 'f'.
    Error using Gui>start_Callback (line 215)
    Error evaluating parameter 'Value' in 'Steifigkeit/k'
    Error using Gui>start_Callback (line 215)
    Undefined function or variable 'k'.

can somebody help me

Upvotes: 0

Views: 3489

Answers (3)

Akanksha
Akanksha

Reputation: 96

I went through your code but there seems to be no function that will update the constant blocks.. I have created a similar GUI to tune inputs at runtime. Try this approach:

In the callbacks of your editboxes in the GUI, write these commands

valstr=get(hObject,'String');
val=str2double(valstr);
assignin('base','nameofvariableinmatlabworkspace',val);
set_param('constantblockpath','Value','nameofvariableinmatlabworkspace');

Replace nameofvariableinmatlabworkspace with the name of the variable that appears inside the constant blocks whose value should change according to the value entered in the GUI. Replace constantblockpath with the path to that constant block e.g. mymodel/Constant12

You might want to do some data validation for the editboxes as well, before assigning their values in the matlab workspace variables.

Upvotes: 0

am304
am304

Reputation: 13876

I think Simulink is looking for k and f in the base workspace, but they are only defined in your callback function workspace. You probably need to use assignin:

function start_Callback(hObject, eventdata, handles)
k= str2double(get(hObject,'string'));
f= str2double(get(hObject,'string'));
assignin('base','f',f);
assignin('base','k',k);
sim('Steifigkeit');

I don't know how your GUI is constructed, but to me it looks like k and f are the same based on your code. Is this how you meant it to be?

Upvotes: 1

NKN
NKN

Reputation: 6414

The general form of the command syntax for running a simulation is:

SimOut = sim('model', Parameters)

So this way you can run simulink models from anywhere using commands. you can also add your parameters to the model and run it.

The following example shows how to create a configuration set and use it with the sim syntax.

model = 'vdp';
load_system(model)
simMode = get_param(model, 'SimulationMode');
set_param(model, 'SimulationMode', 'rapid')
cs = getActiveConfigSet(model);
model_cs = cs.copy;
set_param(model_cs,'AbsTol','1e-5',...
         'SaveState','on','StateSaveName','xoutNew',...
         'SaveOutput','on','OutputSaveName','youtNew')
simOut = sim(model, model_cs);
set_param(model, 'SimulationMode', simMode)

so you just need to replace the values from textbox and that would be this...

I usually use this to set a value in simulink:

Control_Gains(1,1)=str2double(get_param([ModelName,'/PID1/PIDx'],'P'));

or:

NewString               =   ['[',(num2str(KT)),']'];
set_param([ModelName,'/System/Model/Gain'],'Gain',NewString);

Upvotes: 0

Related Questions