MATLAB Answers

Problem plotting 1D array versus table column: I want a single curve/series but am instead getting a separate series for every array element.

1 view (last 30 days)
Srh Fwl
Srh Fwl on 12 Mar 2021
Answered: Seth Furman on 18 Mar 2021
I have used Matlab's "Import Data" facility to import a single column of data from a file. I can plot it by doing this:
plot(myTable.TotalMt)
However, because each element in the imported data represents half a time step, the x axis in the plot is twice as long as I want it to be.
My solution is to create an array called "time" like this:
timeLimit=(size(myTable.TotalMt));
time=1:timeLimit;
time = time./2;
And then plot that with the imported data plus a line representing the average of the imported data:
plot(time,myTable.TotalMt); hold on
mean_simple = mean(myTable.TotalMt,1);
plot(time,mean_simple);
There's a problem though. I have over 200000 series in the plot. It seems that each row of the array and imported table column is being plotted as a separate series. I'm not that experienced in Matlab and if anyone has advice on how to do this the right way, I would be grateful.

Answers (1)

Seth Furman
Seth Furman on 18 Mar 2021
I'd first recommend converting your table into a timetable so you can see the time step for each row.
For example,
>> t = table2timetable(table((1:10)'),'SampleRate',2)
t =
10×1 timetable
Time Var1
_______ ____
0 sec 1
0.5 sec 2
1 sec 3
1.5 sec 4
2 sec 5
2.5 sec 6
3 sec 7
3.5 sec 8
4 sec 9
4.5 sec 10
You can then plot your data directly using stackedplot.
stackedplot(t)
It sounds like myTable.TotalMt has multiple columns. By default the plot and stackedplot functions will plot these separate columns as multiple lines. To plot a single column of a table variable you can split that column into multiple variables with splitvars.
For example,
>> t = timetable([(1:10)' (2:11)'],'SampleRate',2)
t =
10×1 timetable
Time Var1
_______ ________
0 sec 1 2
0.5 sec 2 3
1 sec 3 4
1.5 sec 4 5
2 sec 5 6
2.5 sec 6 7
3 sec 7 8
3.5 sec 8 9
4 sec 9 10
4.5 sec 10 11
>> t = splitvars(t,'Var1')
t =
10×2 timetable
Time Var1_1 Var1_2
_______ ______ ______
0 sec 1 2
0.5 sec 2 3
1 sec 3 4
1.5 sec 4 5
2 sec 5 6
2.5 sec 6 7
3 sec 7 8
3.5 sec 8 9
4 sec 9 10
4.5 sec 10 11
>> stackedplot(t,"Var1_1")

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by