Can I create stackedplot with arrays of different row lengths?
2 views (last 30 days)
Show older comments
Hi,
I’m trying to compare three sets of data, all with different amounts of data, but all associated with the same days.
Data Sets Example:
Tbl1 = [‘Date’ ’tides’ ; ’06-03’ -0.02 ; ’06-03’ 2.0 ; ’06-03’ 0.07 ; ’06-04’ -0.9 ; ’06-04’ 2.3 ; ’06-04’ 0.7 ; ’06-05’ -0.1 ; …]
Tbl2 = [‘Date’ ’temp’ ; ’06-03’ 17 ; ’06-03’ 20 ; ’06-04’ 15 ; ’06-04’ 21 ; ’06-05’ 15 ; ’06-05’ 22 ; ’06-06’ 14 ; …]
Tbl3 = [‘Date’ ’ESD’ ; ’06-03’ 0.2 ; ’06-04’ 0.5 ; ’06-05’ 0.4 ; ’06-06’ 0.1 ; ’06-07’ 0.1 ; ’06-08’ 0.5 ; ’06-09’ 0.4 ; …]
I can’t easily overlap three graphs with very different y-axis on one plot, so I thought stackedplot was my next best bet.
figure (1)
stackedplot(Tbl1, Tbl2, Tbl3)
When I try to graph I get an error: X must have length equal to the number of rows in Y. I think this means my sets of data are all different lengths.
Any suggestions? Thanks!
3 Comments
Walter Roberson
on 12 Mar 2025
Tbl1 = [‘Date’ ’tides’ ; ’06-03’ -0.02 ; ’06-03’ 2.0 ; ’06-03’ 0.07 ; ’06-04’ -0.9 ; ’06-04’ 2.3 ; ’06-04’ 0.7 ; ’06-05’ -0.1 ; …]
Note that this uses [] to concatenate character vectors and numeric constants. According to the conversion rules for [], the numeric constants will be converted by using char(uint16()) applied to the numeric constant, so you are going to end up with results along the lines of
'Datetides'
'06-03<char 0>'
'06-03<char 2>'
'06-03<char 0>'
which is then going to fail because the rows are not all the same number of characters.
You would have been much better off if you used Tabl1 = { ....}
dpb
on 13 Mar 2025
"You would have been much better off if you used Tabl1 = { ....}"
or even better to have read the data from a text or spreadsheet file instead with much more amenable tools for editing data.
Answers (2)
dpb
on 11 Mar 2025
Moved: dpb
on 11 Mar 2025
2 Comments
dpb
on 12 Mar 2025
Of course it can be done in MATLAB and much more simply than in Excel...
Walter shows probably the better way if the difference is that there are different timestamps between the three and not just that some are shorter than the others.
There's a fundamental issue in your data as posted, however, in that you have Date data only to the day and multiple values of the same day wiithout the time information to go with it. Thus, all of those are going to fall exactly at 00:00:00 on the given day, not be different times of day. That's an issue only you know how you may be able to deal with getting the needed times.
To illustrate ways here is problematical because
Tbl1 = [‘Date’ ’tides’ ; ’06-03’ -0.02 ; ’06-03’ 2.0 ; ’06-03’ 0.07 ; ’06-04’ -0.9 ; ’06-04’ 2.3 ; ’06-04’ 0.7 ; ’06-05’ -0.1 ];
Tbl2 = [‘Date’ ’temp’ ; ’06-03’ 17 ; ’06-03’ 20 ; ’06-04’ 15 ; ’06-04’ 21 ; ’06-05’ 15 ; ’06-05’ 22 ; ’06-06’ 14 ; …]
Tbl3 = [‘Date’ ’ESD’ ; ’06-03’ 0.2 ; ’06-04’ 0.5 ; ’06-05’ 0.4 ; ’06-06’ 0.1 ; ’06-07’ 0.1 ; ’06-08’ 0.5 ; ’06-09’ 0.4 ; …]
even if we try to remove the "..." line continuation character at the end of the line(s).
Attaching the actual file would probably help...
Walter Roberson
on 11 Mar 2025
First convert the tables into timetables, which might require converting the dates into datetime objects.
Then form the union of the datetime objects between the three tables.
Now, retime each of the tables, using the union of times as the newTimes parameter, and method 'fillwithmissing'
Now that you have a set of three time tables with the same time base, you can stackedplot them together.
0 Comments
See Also
Categories
Find more on Line Plots 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!