How to plot a string signal/array?
9 views (last 30 days)
Show older comments
Hi all,

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 :

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 Comments
Accepted Answer
Chuguang Pan
on 9 Jan 2025
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 Comments
More Answers (1)
Paul
on 9 Jan 2025
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])
See Also
Categories
Find more on Time Series Events in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!