How to plot a string signal/array?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Seigan
am 9 Jan. 2025
Kommentiert: Chuguang Pan
am 9 Jan. 2025
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 Kommentare
Akzeptierte Antwort
Chuguang Pan
am 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 Kommentare
Weitere Antworten (1)
Paul
am 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])
Siehe auch
Kategorien
Mehr zu Time Series Events finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!