在 gui 中绘制 simout
Plotting simout in gui
我正在 Simulink 中模拟投掷:
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
当使用输出参数调用时,sim
函数 returns a Simulink.SimulationObject
(它是一个对象,但就像一个结构,其字段是在To Workspace
块)。
所以,至少你需要
x = simulace.get('simout');
y = simulace.get('simout1');
在你的情况下,你可能拥有 simout
和 simout1
的数据类型 timeseries
,因此你通常不会将它们相互绘制,而这可能会导致其他错误。
如果您没有 timeseries
,或者它们是 timeseries
但您确实想将它们相互绘制,那么您将需要更深入地挖掘这些变量以提取您要绘制的特定数据。
例如,如果它们是 两者 timeseries
,但您想绘制 数据 包含在它们之间,然后在上面你会做:
plot(x.Data,y.Data);
这假设 x.Data 是一个向量。 (如果不是,则无论如何都不能将其用作绘图 x 轴的数据。)
我还建议您使用调试器来解决您的具体问题。在回调中打断点,运行 调试器,查看回调中使用的数据。
我正在 Simulink 中模拟投掷:
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
当使用输出参数调用时,sim
函数 returns a Simulink.SimulationObject
(它是一个对象,但就像一个结构,其字段是在To Workspace
块)。
所以,至少你需要
x = simulace.get('simout');
y = simulace.get('simout1');
在你的情况下,你可能拥有 simout
和 simout1
的数据类型 timeseries
,因此你通常不会将它们相互绘制,而这可能会导致其他错误。
如果您没有 timeseries
,或者它们是 timeseries
但您确实想将它们相互绘制,那么您将需要更深入地挖掘这些变量以提取您要绘制的特定数据。
例如,如果它们是 两者 timeseries
,但您想绘制 数据 包含在它们之间,然后在上面你会做:
plot(x.Data,y.Data);
这假设 x.Data 是一个向量。 (如果不是,则无论如何都不能将其用作绘图 x 轴的数据。)
我还建议您使用调试器来解决您的具体问题。在回调中打断点,运行 调试器,查看回调中使用的数据。