【问题标题】:Plotting simout in gui在 gui 中绘制 simout
【发布时间】:2016-04-23 09:57:10
【问题描述】:

我在 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

【问题讨论】:

    标签: matlab plot simulink


    【解决方案1】:

    当使用输出参数调用时,sim 函数返回 Simulink.SimulationObject(它是一个对象,但类似于具有字段的结构,这些字段是在 To Workspace 块中指定的变量名称)。

    所以,至少你需要

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

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

    如果您没有将它们设置为 timeseries,或者它们是 timeseries,但您确实希望将它们相互绘制,那么您将需要更深入地挖掘这些变量以提取您需要的特定数据'想出谋划策。

    例如,如果它们是 both timeseries,但您想将它们中包含的 数据 相互绘制,那么完成上述操作后:

    plot(x.Data,y.Data);
    

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

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

    【讨论】:

    • 是的 simout 是时间序列...如果我按照你说的做,我会得到同样的错误,但这次是 "x = simulace.get('simout');"我应该将时间序列更改为数组吗?
    • 我添加了一些行来展示如何将时间序列与 plot 一起使用。
    • 已解决(已编辑问题),我不需要 .get,但无论如何,请咨询您。
    • 是的,.get 是 Mathworks 推荐的方法,但不是必需的。
    猜你喜欢
    • 2013-04-11
    • 2018-01-19
    • 2023-03-24
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多