How to plot a string signal/array?
7 ビュー (過去 30 日間)
古いコメントを表示
Hi all,
![Simulink String](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1822978/Simulink%20String.png)
mySignal is a string signal, and not a numeric one. I log it into the base workspace inside the simout variable, and I also send it to the Simulation Data Inspector.
Inside the Simulation Data Inspector, this string signal is neatly displayed, like this :
![Simulation Data Inspector](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1822979/Simulation%20Data%20Inspector.png)
The string values are displayed as colored patches, evolving with the simulation time.
This visualization method for string signals is great, but I can't find a way to do something similar in MATLAB. Once my string signal is inside a base workspace variable, I try to plot it just like a regular signal, but it doesn't work:
>> simout
simout =
struct with fields:
time: [1001×1 double]
signals: [1×1 struct]
blockName: 'plot_string_signals/To Workspace'
>> simout.signals
ans =
struct with fields:
values: [1001×1 string]
dimensions: 1
label: 'mySignal'
>> plot(simout.time, simout.signals.values)
Error using plot
Invalid data argument.
So my question is: how to plot a string datatype signal/array in MATLAB? Is there a dedicated plotting function for this use case that I don't know ? I expected the plot function to provide the same kind of graph as the Simulation Data Inspector, but was disappointed.
(I'm using MATLAB 22b Update 8)
Thanks,
Seigan
3 件のコメント
採用された回答
Chuguang Pan
2025 年 1 月 9 日
In the Documentation, the Simulink.sdi.plot function can plot different objects exported from simulations, it can reproduce the plot effect of strings as illustrated in your Simulation Data Inspector.
0 件のコメント
その他の回答 (1 件)
Paul
2025 年 1 月 9 日
I don't believe there is a built-in function to plot a string vector against a numeric vector in any fashion, much less to produce the plot in the data inspector.
I've never used Simulation Data Inspector. Maybe it has an option to generate and provide m-code for the plot that it produces?
Writing code to generate that plot deosn't seem like it would be too hard, at least the basic appearance.
Here's something for starters. You'll have to decide how/if to display the final element in data
time = 0:5;
data = ["Hello","World","Hello","World","Hello","World"];
figure
for ii = 1:numel(time)-1
p(ii) = patch([time(ii),time(ii+1),time(ii+1),time(ii)],[0 0 0.2 0.2],'r','FaceAlpha',0.2);
tx(ii) = text(time(ii)+0.05,0.1,data(ii));
end
ylim([0 1])
参考
カテゴリ
Help Center および File Exchange で Time Series Events についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!