在gui中绘制simout

时间:2016-04-23 09:57:10

标签: matlab plot simulink

我正在模拟Simulink中的一次投掷: y position

同样适用于x位置 我创建了gui,你在文本字段中写变量,然后单击sim,然后gui应该显示该情节,但我收到此错误:

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

这是我的代码:

% --- 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)

请问有什么问题?

编辑(它的工作方式如下+我添加了着色,如果有人被编辑):

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

1 个答案:

答案 0 :(得分:1)

当使用输出参数调用时,sim函数返回Simulink.SimulationObject(它是一个对象,但就像一个结构,其字段是To Workspace块中指定的变量名称)。

所以,至少你需要

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

在您的情况下,您可能会将simoutsimout1的数据类型设置为timeseries,因此您通常不会相互映射它们,这样可能会导致其他错误。

如果你没有将它们作为timeseries,或者它们是timeseries,但你确实希望将它们相互映射,那么你需要深入研究这些变量以便提取您想要绘制的具体数据。

例如,如果他们 两者 timeseries,但您想要将彼此包含的数据相互映射,然后在上面你做了:

plot(x.Data,y.Data);

这假定x.Data是一个向量。 (如果不是,那么无论如何都不能将它用作绘图x轴的数据。)

我还建议您使用调试程序来解决您的具体问题。在回调中放置一个断点,运行调试器,并查看回调中使用的数据。