Search code examples
matlabplotsimulink

Plotting simout in gui


I am simulating a throw in Simulink: y position

Likewise for x position I created gui where you write variables in text fields and then click sim, then the gui should show the plot, but I am getting this error:

Attempt to reference field of non-structure array.

Error in timeseries/plot (line 34)
dataContent = h.Data;

Error in timeseries/plot (line 135)
    p = plot(ax,Time,Data,varargin{:});

Error in asd>sim_Callback (line 162)
plot(x,y);

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

Error in asd (line 42)
    gui_mainfcn(gui_State, varargin{:});

Error in
@(hObject,eventdata)asd('sim_Callback',hObject,eventdata,guidata(hObject))


Error while evaluating uicontrol Callback

This is my code:

% --- Executes on button press in sim.
function sim_Callback(hObject, eventdata, handles)
% hObject    handle to sim (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
k = 0.0134;
g = 9.81;
m = str2num(get(handles.edit1,'String'));
v_0 = str2num(get(handles.edit2,'String'));
a = degtorad(str2num(get(handles.edit3,'String')));
b = cos(a);
c = sin(a);
axes(handles.graf);
options = simset('SrcWorkspace','current');
simulace = sim('simulnikSem',[],options);
x=simout;
y=simout1;
plot(x,y);
hold on


% --- Executes on button press in clear.
function clear_Callback(hObject, eventdata, handles)
% hObject    handle to clear (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
cla(handles.graf)

What is the problem please?

Edit(Its working like this+I added coloring, if someone is intreseted):

global p;
cm = lines(20);
k = 0.0134;
g = 9.81;
m = str2num(get(handles.edit1,'String'));
v_0 = str2num(get(handles.edit2,'String'));
a = degtorad(str2num(get(handles.edit3,'String')));
b = cos(a);
c = sin(a);
axes(handles.graf);
options = simset('SrcWorkspace','current');
simulace = sim('simulnikSem',[],options);
x = simout;
y = simout1;
what = cm(p,:);
plot(x.Data,y.Data,'color',cm(p,:),'LineWidth',2);
p = p+1;
hold all

Solution

  • When called with an output argument, the sim function returns a Simulink.SimulationObject (which is an object, but like a structure with fields that are the variable names specified in the To Workspace blocks).

    So, at a minimum you need

    x = simulace.get('simout');
    y = simulace.get('simout1');
    

    In your case it looks like you might have the datatype of simout and simout1 as being timeseries, so you wouldn't normally plot them against each other, and this might cause other errors.

    If you don't have them as timeseries, or they are timeseries but you do want to plot them against each other, then you will need to dig deeper into those variables to extract the specific data that you're wanting to plot.

    For instance, if they are both timeseries, but you want to plot the data contained in them against each other, then after the above you'd do:

    plot(x.Data,y.Data);
    

    This assumes that x.Data is a vector. (If it's not then you can't use it as data for the x-axis of a plot anyway.)

    I'd also recommend that you use the debugger to sort out your specific issue. Put a break point in the callback, run the debugger, and look at the data being used in the callback.