How to plot a string signal/array?

7 ビュー (過去 30 日間)
Seigan
Seigan 2025 年 1 月 9 日
コメント済み: Chuguang Pan 2025 年 1 月 9 日
Hi all,
In Simulink, I'm using String Signals to represent internal states for my systems. For example:
Simulink String
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
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 件のコメント
Seigan
Seigan 2025 年 1 月 9 日
@Chuguang Pan Wow it works great, thank you! If I store my string array as a timeseries object, I can indeed feed it to Simulink.sdi.plot and it works as wanted. Could you repost your comment as an answer so that I can mark it as "Accepted" ?
Chuguang Pan
Chuguang Pan 2025 年 1 月 9 日
@Seigan Thanks for your reply, I have reposted my comment as an answer.

サインインしてコメントする。

採用された回答

Chuguang Pan
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.

その他の回答 (1 件)

Paul
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])
  1 件のコメント
Seigan
Seigan 2025 年 1 月 9 日
Thank you for this solution @Paul.
I was hoping for an optimized, built-in solution, because my string arrays can be large and I may want to plot several string arrays on the same graph.
@Chuguang Pan's solution works better for me, because it invokes the Simulation Data Inspector window (even if Simulink is not opened), which I'm fine using instead of a classical figure/plot to view my string signals.
I'm hoping @MathWorks Support Team could maybe consider this feature request (= update the plot function to accept string arrays and produce a graph inspired by the way Simulation Data Inspector handles this) for a future release. It would help people who work with string arrays.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTime Series Events についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by