How to sum two y axis data
显示 更早的评论
Hello
I have done 2 plots(see attached files) Now I need to sum my the 2 curves in one graph. As a result it should be one curve with y axis values added to each other. Such as in the year 45 1 graph y value 87,2 graph y value 80. So my new value should appear as 167.
Thanks a lot!


采纳的回答
It appears to me you only need a single y axes as the two plots appear to have the same range. To get two line plots on the same axes,
plot(....) %draw the first one
hold on
plot(....) %draw the second one
hold off
11 个评论
Thanks Walter, sorry my explanation was not good. I need sum 2 y datas. if I do as you told I will get 2 curve in 1 graph, but I need 1 curve.(summing two y curve datas)
Are the x and y values known, or do they need to be extracted from the graphic?
Are the x values the same for the parts that overlap, from roughly about 6 to 49?
How do you want to handle the left and right edges of the second graph, which extend further than the first graph? I estimate that from about 3.5 to 6 on the left and about 49 to 50 on the right has no corresponding data shown on the upper graph.
davit petraasya
2016-1-25
编辑:Stephen23
2016-1-25
Hi Walter! My x and y values known (attached files).x values different for the parts overlap. So my result graph should include all data from 1 and 2 graph. If in second graph has no corresponding point in first graph my result graph should come without changing, it should come all points.(it should not be any missing point).The result graph should start from 3.5 till 50.
Thank you very much for your help!
You can use max() of the two beginning bounds to find the starting point of the overlap, and min() of the two end points to find the end point of the overlap. From each, extract the range that is in common to both. Then take the union() of those two to get all of the sample points together. Use that common set of points as target points for interp1() on each of the two, and add the two results together.
I have been up all night so I think I will skip showing the code. Pretty much like http://www.mathworks.com/matlabcentral/answers/256459-checking-ismember-intersect-for-lot-of-arrays
Great. Thanks Walter!
davit petraasya
2016-1-27
编辑:davit petraasya
2016-1-27
Hi! It would be great if you someone can help me on this issue. Last time Walter helped me a lot, but still I did not get result. So my problem is I have 2 excel tables (see attached files, you can see my plots on my first message above ).I need to sum my 2 y values data and get one result curve. The size of the 2 y also different. One point is my result graph should start from zero(year). (on new graph -my previous 1-graph(serre pancon) year 1960 point should be equal to 0, and on my previous 2-graph 1959 year should be correcponding to 0 year on new result graph. Binning would be yearly. Thanks a lot!
all_times = unique([first_times(:); second_times(:)]);
first_projected = interp1(first_times, first_data, all_times, 'extrap', nan);
second_projected = interp1(second_times, second_data, all_times, 'extrap', nan);
first_projected(isnan(first_projected)) = 0;
second_projected(isnan(second_projected)) = 0;
data_total = first_projected + second_projected;
plot(all_times, data_total)
You can subtract all_times(1) from all_times if you want the earliest time between the two to correspond to 0.
If you want to align the first data relative to the beginning of 1960 and the second data relative to the beginning of 1959 and you want the relative positions to be considered the same even though the years are different, then before this code,
first_times = first_times - datenum('1960/1/1');
second_times = second_times - datenum('1959/1/1');
Hi Walter. Thank you so much for the code! It helps:)
Thank you once more for the code Walter. It works well. One last thing actually I had 28 table for summing values. For some tables it appears error -The values of X should be distinct. Is there any way to get rid of the problem? Thanks a lot!
Can you please give me soon the full code? I also have same problem and want it urgently. Will be waiting for yours reply. Thank you.
davit petraasya is probably not following this topic any longer, as it is a year old.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Animation 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
