How can I plot sequential data of a matrix

Hello, I've derived my data from a software and it gives the data in a sequential format. It's a 2-column matrix in which the first column is X and the second one is Temperature of that point. X starts from 0 to 0.2, then I have another 0 to 0.2 and this pattern repeats. The first X from 0 to 0.2 and its pertaining T are corrosponding values for first time step and the next one is for the next time step, ... . The number of rows are not fixed for every iteration and total number of rows are unknown. I want to plot X vs T for every time step. Does anyone have an idea for defining the matrix?
0 329.62
0.01 329.61
0.014 329.60
0.2 329.59
0.00 329.63
0.05 329.62
0.10 329.64
0.15 329.68
0.20 3295
0 329
...

 Respuesta aceptada

Star Strider
Star Strider el 18 de Abr. de 2021
Try this:
A = [0 329.62
0.01 329.61
0.014 329.60
0.2 329.59
0.00 329.63
0.05 329.62
0.10 329.64
0.15 329.68
0.20 329.5
0 329];
is0 = find(A(:,1) == 0);
for k = 1:numel(is0)-1
idxrng = is0(k):is0(k+1)-1
segment{k} = A(idxrng,:);
end
figure
hold on
for k = 1:numel(segment)
plot(segment{k}(:,1), segment{k}(:,2), '.-')
end
hold off
grid
xlabel('X')
ylabel('T')
I have no idea how robust it will be for the full data set, however it works on the sample posted.

8 comentarios

Amir Mohammadi
Amir Mohammadi el 18 de Abr. de 2021
Editada: Amir Mohammadi el 18 de Abr. de 2021
Hi,
First off, thank you very much for your response. I changed the A values to be actual data and it worked. The reason I wanted the segments separately was to calculate the average of these individual Temperature distributions over X, assuming a is the first segment and z the last one: Tavg=a+...+z/(number of data segments), leading to a single T vs X diagram, but when I want to calculate the average over the segment{k}(:,2) the result would be an average over the individual data set not over the entire data. Do you think I could use the segment{K}(:,2) to find the average?
thanks,
  • Plot for actual data :
Average over the segment{k}(:,2):
As always, my pleasure!
Yes.
One option is to calculate the averages in the first loop:
for k = 1:numel(is0)-1
idxrng = is0(k):is0(k+1)-1;
segment{k} = A(idxrng,:);
segment_mean(k) = mean(segment{k}(:,2));
end
It is still possible to plot the mean values in the second loop, as well as plotting the segments themselves. (I have no idea how you are plotting the mean values in the second plot. I would suspect they would be a vertical line of points at some value of ‘X’.)
Amir Mohammadi
Amir Mohammadi el 18 de Abr. de 2021
Hi,
The problem with my average plot was because I calculated it within second loop instead of the first one which I am not sure why lead to those bizzarre vertical lines. My issue still with the Mean Command is the fact that it calculate the average of the entire segment, leading to a horizontal line. I am looking for local average of the values, though, and by local, I mean determining the average of T at every single X. The result should be something like the plot below. I separated few segments one by one manually, then using Matlab's polyfit feature, calculated the average by summing up these individual graphs and dividing them to the number of plots. As you could see in the second plot the graphs are not completely accurate in comparison with the first picture in my first response since they are fitted, but it was the only solution I could think of and it as you may guess it is not possible to apply this approch to all data set I have. Anyways, thanks a lot as your responses were really helpful and I learnt many things from them.
Star Strider
Star Strider el 18 de Abr. de 2021
If you want to calculate the mean values over an inconsistently-sampled array of data, it would be necessary to interpolate each segment dependent variable data to the same independent variable data. This ie straightforward with interp1, however it would need to be done on each segment (preferably in the first loop), and then stored in a separate array. Then it would be possible to calculate the mean values at every interpolated value.
Amir Mohammadi
Amir Mohammadi el 18 de Abr. de 2021
Alright, I will try that. Thanks
As always, my pleasure!
That entire code would go something like this:
A = [0 329.62
0.01 329.61
0.014 329.60
0.2 329.59
0.00 329.63
0.05 329.62
0.10 329.64
0.15 329.68
0.20 329.5
0 329];
is0 = find(A(:,1) == 0);
intrp_x = linspace(0, 0.2, 10).'; % Interpolation X-Vector
for k = 1:numel(is0)-1
idxrng = is0(k):is0(k+1)-1;
segment{k} = A(idxrng,:);
segment_mean(k) = mean(segment{k}(:,2));
segment_intrp{k} = interp1(A(idxrng,1), A(idxrng,2), intrp_x); % Interpolate
end
intrp_means = mean(cell2mat(segment_intrp),2); % Mean Of Interpolated Values
figure
hold on
for k = 1:numel(segment)
plot(segment{k}(:,1), segment{k}(:,2), '.-', 'DisplayName',sprintf('Segment %d',k))
end
plot(intrp_x, intrp_means, '--k', 'DisplayName','Mean of Interpolated Values')
hold off
grid
xlabel('X')
ylabel('T')
legend('Location','best')
Experiment to get the result you want.
Amir Mohammadi
Amir Mohammadi el 19 de Abr. de 2021
That's awesome. As always, Thank you!
Star Strider
Star Strider el 19 de Abr. de 2021
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 18 de Abr. de 2021

Comentada:

el 19 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by