Can I create stackedplot with arrays of different row lengths?

3 次查看(过去 30 天)
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-032.0 ; 06-030.07 ; 06-04-0.9 ; 06-042.3 ; 06-040.7 ; 06-05-0.1 ; ]
Tbl2 = [Date’ ’temp; 06-0317 ; 06-0320 ; 06-0415 ; 06-0421 ; 06-0515 ; 06-0522 ; 06-0614 ; ]
Tbl3 = [Date’ ’ESD; 06-030.2 ; 06-040.5 ; 06-050.4 ; 06-060.1 ; 06-070.1 ; 06-080.5 ; 06-090.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 个评论
Walter Roberson
Walter Roberson 2025-3-12
Tbl1 = [Date’ ’tides; 06-03-0.02 ; 06-032.0 ; 06-030.07 ; 06-04-0.9 ; 06-042.3 ; 06-040.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
dpb 2025-3-13
"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.

请先登录,再进行评论。

回答(2 个)

dpb
dpb 2025-3-11
移动:dpb 2025-3-11
Augment each shorter dataset to the length of the longest with NaT for the time and NaN for the data. The plotting routines will then ignore the missing data points but be the same length to be able to put together in one table or catenate arrays.
  2 个评论
Kristine
Kristine 2025-3-11
Is there any way to do this in MATLAB, or would I have to do this by hand via Exel? It would take me a while to spread out the data and create gaps for where the shorter data sets don't have enough information.
dpb
dpb 2025-3-12
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-032.0 ; 06-030.07 ; 06-04-0.9 ; 06-042.3 ; 06-040.7 ; 06-05-0.1 ];
Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII characters.
Tbl2 = [Date’ ’temp; 06-0317 ; 06-0320 ; 06-0415 ; 06-0421 ; 06-0515 ; 06-0522 ; 06-0614 ; ]
Tbl3 = [Date’ ’ESD; 06-030.2 ; 06-040.5 ; 06-050.4 ; 06-060.1 ; 06-070.1 ; 06-080.5 ; 06-090.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
Walter Roberson 2025-3-11
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.

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by