MATLAB 2017中Simulink导出Scope数据绘图报错求助
Ah, I’ve dealt with this exact headache in MATLAB R2017 before! The error almost always comes down to how your Structure with time is structured, especially the dimensions of the signal data or how you’re accessing it. Let’s break down the fixes step by step:
1. First, Diagnose the Structure of Your X Variable
Start by typing X in the MATLAB command window to inspect what you’re working with. You need to check two key things:
X.time: Should be a column vector (e.g.,1000x1for 1000 time steps). If it’s a row vector, you might need to transpose it later, but Simulink usually outputs it as a column by default.X.signals: This is either a single struct (for one signal) or a struct array (for multiple signals). If it’s an array (look forX.signals(1),X.signals(2)), you can’t plotX.signals.valuesdirectly—you have to index into individual signals.
Run size(X.signals.values) too. If it returns something like 1x1000 instead of 1000x1, that’s a problem because plot expects the Y-data to match the length of X.time (which is a column).
2. Fix the Plot Command Based on Signal Count
Case 1: You have one signal
If X.signals is a single struct, check if X.signals.values is a row vector. If so, transpose it to match X.time:
plot(X.time, X.signals.values')
If it’s already a column vector, the original command should work—but double-check that there’s no extra dimension (like a 3D array, which plot can’t handle).
Case 2: You have multiple signals
X.signals will be an array of structs. You need to plot each signal individually by indexing into the array:
% Plot first signal in red, second in blue plot(X.time, X.signals(1).values, 'r', X.time, X.signals(2).values, 'b')
Trying to use X.signals.values directly will pass a comma-separated list of vectors to plot, which throws the error you’re seeing.
3. Adjust Simulink Configuration Settings
Let’s make sure your model is exporting data correctly in the first place:
- Open your model’s Configuration Parameters (press
Ctrl+E). - Go to the Data Import/Export tab:
- Ensure Time and Output (or the specific signals you want to log) are checked.
- Set Save format to
Structure with time.
- If you’re using a
To Workspaceblock (e.g.,simout):- Double-click the block, set Save format to
Structure with time. - Under Save 2-D signals as, select
2-D array(this ensures single signals are saved as column vectors, not row vectors). - Uncheck Limit data points to last unless you intentionally want truncated data (this can cause mismatched time and signal lengths).
- Double-click the block, set Save format to
4. Handle Bus Signals (If Applicable)
If you’re logging a bus signal, the Structure with time will have nested structs. For example, if your bus has two signals speed and torque, you’ll access them like this:
plot(X.time, X.signals.values.speed, 'g', X.time, X.signals.values.torque, 'm')
Make sure you’re drilling down to the actual signal values instead of trying to plot the nested struct directly.
内容的提问来源于stack exchange,提问作者emic

