Dimensionality error with plot

Hi, I would like to plot a snippet of pressureData, which is a 3 dimensional matrix, as follows:
tDim=length(pressureData(1,1,:)); t=zeros(1,tDim); dt=0.01;
for ii=1:tDim % Build time axis for plot
t(ii)=ii*dt;
end
plot(t,pressureData(1,1,:))
This returns an error saying "Error using plot. Data cannot have more than two dimensions."
When I output the vector pressureData(1,1,:) in the command window, the output looks like this:
ans(:,:,1) =
-0.0038
ans(:,:,2) =
-0.0042
etc.
The data type for pressureData is double, so I'm not sure why I am getting this odd output. Any ideas? Thanks in advance!

5 comentarios

VBBV
VBBV el 20 de Jun. de 2023
Movida: VBBV el 20 de Jun. de 2023
 Pdata = reshape(pressureData(1,1,:),1,[])
plot(t,Pdata)
VBBV
VBBV el 20 de Jun. de 2023
Movida: VBBV el 20 de Jun. de 2023

Use reshape function to reduce the multidimensional matrix into vector since plot function uses vectors to graph data

Andrew
Andrew el 20 de Jun. de 2023
Movida: VBBV el 20 de Jun. de 2023
@VBBV that does fix the plotting issue. How would I then reform the matrix back into its original 3D configuration?
Andrew
Andrew el 20 de Jun. de 2023
The pressureData matrix is a 50x50x5000 matrix and I would like to keep it that way.
the cyclist
the cyclist el 20 de Jun. de 2023
Neither of the solutions so far (as I write this) change the size of pressureData.

Iniciar sesión para comentar.

 Respuesta aceptada

the cyclist
the cyclist el 20 de Jun. de 2023
Editada: the cyclist el 20 de Jun. de 2023
I understand your confusion. pressureData(1,1,:) is a vector in the sense that it only has elements along one dimension (the 3rd dimension), but it is still a three-dimensional MATLAB array because it is 1x1xtDim.
There are a couple possible solutions. Probably the easiest is to use the squeeze function to remove the dimensions that have length 1.
plot(t,squeeze(pressureData(1,1,:)))
You could also use the reshape or permute functions to manipulate the shape of the array.

3 comentarios

Andrew
Andrew el 20 de Jun. de 2023
Editada: Andrew el 20 de Jun. de 2023
@the cyclist got it. Thank you!
the cyclist
the cyclist el 20 de Jun. de 2023
Editada: the cyclist el 20 de Jun. de 2023
The size of pressureData doesn't really matter. The fact remains that
pressureData(1,1,:)
is a 1x1x5000 array, which is 3-dimensional. MATLAB will not treat that as a vector for plotting purposes, which is why you get the error. But
squeeze(pressureData(1,1,:))
is a 5000x1 vector, which will plot.
Andrew
Andrew el 20 de Jun. de 2023
@the cyclist thank you!

Iniciar sesión para comentar.

Más respuestas (1)

VBBV
VBBV el 20 de Jun. de 2023
Editada: VBBV el 20 de Jun. de 2023
Pdata = reshape(pressureData(1,1,:),1,[])
plot(t,Pdata)

2 comentarios

VBBV
VBBV el 20 de Jun. de 2023
Editada: VBBV el 20 de Jun. de 2023

The original 3D remains in the pressureData variable itself visible in workspace. For plotting purposes a variable is assigned (i.e. Pdata in my answer) after reshaping the data. You can directly apply the reshape function on presssureData itself.

Andrew
Andrew el 20 de Jun. de 2023
@VBBV thank you!

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Etiquetas

Preguntada:

el 20 de Jun. de 2023

Comentada:

el 20 de Jun. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by