How can I plot multiple graphs from one set of table data, separated by NaN?

1 次查看(过去 30 天)
I have a table as below: M=
NaN NaN
2 3
NaN NaN
NaN NaN
15 6
6 7
3 12
38 17
9 59
NaN NaN
10 129
11 34
9 5
NaN NaN
NaN NaN
NaN NaN
Goal: I am trying to plot graphs from each set of data, that is separated by the NaNs
E.g. for the data below a graph with x=2, y=3 Then a graph with x=15, 6, 3, 38, 9, y=6, 7, 12, 17, 59 respectively
Overall goal: to find the slope of each set of data and plot the slopes as a frequency distribution
Thank you!

回答(3 个)

Raul Andres Lopez Romero
You can do this,
first we need to do a logical index for the NaN values,
M= ... %(your data values);
M1=M(1:end,1)
M2=M(2:end,2)
The Index for logical values are (isnan, check documentation for more info)
idxsM1 = ~isnan(M1);
*|*so the index show this*|*
0
1
0
0
1
1
1
1
1
0
1
1
1
0
0
0
M1noNaN=M1(idxsM1)
so you can plot because all NaN values are not in the vector.
plot(M1noNaN)
hold on
plot(M1noNaN-detrend(M1noNaN))
% So detrend (a-detrend(a)) plot the mean value or, slope im not sure check documentation for more info hold off
if you want the vector of detrend sM1=detrend(M1noNaN)
Do the same for vector M2 or second column of you data set M

Raul Andres Lopez Romero
编辑:Raul Andres Lopez Romero 2017-12-13
I think checking your update, you want something like this
the code for this its simple, you need to generate the second vector without NaNs and do a scatter plot
scatter(M1noNaN,M2noNaN,'filled')
grid on
xticks(0:1:40)
xtickangle(90)
yticks(0:5:140)
and you can check this for the slope
  2 个评论
Shravani Kakarla
Shravani Kakarla 2017-12-13
Hi Raul,
Thank you very much!
This helps quite a bit.
My only issue is that defining (end,1) for M1 = M{1:end,1} for each set of data (e.g. 1st data set for the data above is 2,3) is tricky.
Since I have thousands of data sets separated by NaNs, so defining each end by hand is difficult.
Is there any way to parse the data and have an end defined before each new NaN, for each data set?
So M1 would be 2, 3
M2 would be
15 6
6 7
3 12
38 17
9 59
Raul Andres Lopez Romero
Check the new answer, then, i think you can manage the data and do all the calculations that you need.

请先登录,再进行评论。


Raul Andres Lopez Romero
编辑:Raul Andres Lopez Romero 2017-12-13
% Star with position and values
% A it's your matrix that contains the NaN elements
pos=[true,isnan(A(:,1)).',true];
ini=strfind(pos,[true, false]);
end=strfind(pos,[false, true]) - 1;
M=cell(1,length(ini));
for i = 1:length(ini)
M{i} = A(ini(i):end(i),:);
end
% That creates a M cell vector to contains all the values
% But for generate the M1 and M2 ... Mi vectors you need to transform
%the cell vector in array
M1=cell2mat(M(1,1))
% for M2
M2=cell2mat(M(1,2))
% for M3
M3=cell2mat(M(1,3))
the result is

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by