plotting structure elements and dimensions in matlab - matlab

I'm trying to create a 3D plot in Matlab of a structure element, Bstruct.scen_1. In this structure, each row is a year, each column is a distance, and the cell value is a population size (so for example, row 3, column 7 would yield the number of adults in year 3 at 7 km.) I want the X-axis to be the number of columns in Bstruct.scen_1, the Y-axis to be the actual value in the cell at (X,Z), and the Z-axis to be the number of rows in Bstruct.scen_1.
Conceptually, what I'd like to accomplish is:
plot3(Bstruct.scen_1(1:num_cols), Bstruct.scen_1(cellvalue), Bstruct.scen_1(1:num_rows))
I'm struggling with the syntax of structures and would really appreciate help in plotting both the elements and the dimensions of this structure. (I mostly code in R with 'tidy' data.) Thank you!

The value of a structure field can be any data type. It sounds like the scen_1 field contains a 2D matrix. The plot3 function expects an X, Y, and Z coordinate for each data point. In your case, if you're wanting to plot the value of the matrix at each 2D location, using the function surf (or mesh) might provide a good start:
% random data for demonstration
Bstruct.scen_1 = rand(20, 10);
figure;
surf(Bstruct.scen_1);

Related

Plotting with a matrix in Matlab

The thing is i have a matrix and when i use imagesc() it goes like this but my goal is this.
So my question is does any one know which plot is this or some one has document about this, thanks.
If you have two vectors r and theta that give the polar coordinates, or two matrices rGrid and thetaGrid that give the polar coordinates for each element of the data matrix, then code like this will work:
r=linspace(1,20,20);
theta=linspace(0,2*pi,20);
data = r'.*sin(2.*theta); % INSERT DATA HERE
[thetaGrid,rGrid]=meshgrid(theta,r); % Create coordinate grid if needed
[xGrid,yGrid]=pol2cart(thetaGrid,rGrid);
surf(xGrid,yGrid,data); % Plot data
view(2);
Just keep in mind that the rows of the data matrix need to correspond to different radii, and columns need to correspond to different values of theta. If it's flipped, then transpose the matrix before plotting:
data = data';
Also, if the data doesn't wrap around from 0 to 2*pi radians, then repeat the first value of theta as the last value, and repeat the first column of the data matrix as a new final column:
theta(end+1)=theta(1);
data=cat(2,data,data(:,1));
There is also a 3D Polar Plot function on the MATLAB file exchange, but I do not have any experience using it: 3D Polar Plot

Using MATLAB, is there a way to get a 2D visualisation of data points that are in 6D space?

Part of an assignment of mine is to provide a 2D visualisation (plots) of some of my data points that are stored in a matrix. I'm slightly confused because the data is actually in 6D space (i.e. each row has 6 columns like 0 1 0 8 8 2).
Is there something I'm missing or does this genuinely not make sense? Is this something MATLAB can do?
Edit: Is something like this possible?
Though I wouldn't consider it visualizing 6D data, you can get the linked plot with a simple call to plot:
A = rand(6);
x = 1:6;
plot(x,A'); % Transpose A to plot rows since it's square, see plot documentation
Which produces the following:
From the documentation:
If one of X or Y is a vector and the other is a matrix, then the
matrix must have dimensions such that one of its dimensions equals the
vector length. If the number of matrix rows equals the vector length,
then the plot function plots each matrix column versus the vector. If
the number of matrix columns equals the vector length, then the
function plots each matrix row versus the vector. If the matrix is
square, then the function plots each column versus the vector.
Simply use:
surf(2Dmatrix)
You can read more here: http://uk.mathworks.com/help/matlab/ref/surf.html
If your matrix is 2D image, simply use
figure; imshow(2Dmatrix, [])
If you leave the square bracket empty, the limit will be automatic. When the figure is displayed you can change it to different colormap by Edit > Colormap.

Selecting values plotted on a scatter3 plot

I have a 3d matrix of 100x100x100. Each point of that matrix has assigned a value that corresponds to a certain signal strength. If I plot all the points the result is incomprehensible and requires horsepower to compute, due to the large amount of points that are painted.
The next picture examplify the problem (in that case the matrix was 50x50x50 for reducing the computation time):
[x,y,z] = meshgrid(1:50,1:50,1:50);
scatter3(x(:),y(:),z(:),5,strength(:),'filled')
I would like to plot only the highest values (for example, the top 10). How can I do it?
One simple solution that came up in my mind is to asign "nan" to the values higher than the treshold.
Even the results are nice I think that it must be a most elegant solution to fix it.
Reshape it into an nx1 vector. Sort that vector and take the first ten values.
num_of_rows = size(M,1)
V = reshape(M,num_of_rows,1);
sorted_V = sort(V,'descend');
ind = sorted_V(1:10)
I am assuming that M is your 3D matrix. This will give you your top ten values in your matrix and the respective index. The you can use ind2sub() to get the x,y,z.

Matlab divergence of a randomly sampled 3d vector field

I am trying to use the divergence function in Matlab over a dataset which is not ordered. I have therefore the x, y, z positions of the origins of the vectors (3 columns) and the three components Fx, Fy, Fz of the vector field (3 columns), for a total of a 6 columns dataset, where the positions are random points in a 3d volume. How should I transform the data in order to be readable by divergence?
I guess I should use meshgrid and generate an ordered grid associated to my original random points, but how should I deal with the vector field F?
You are correct, use a meshgrid to generate the uniform datapoints, and then if your data is well behaved, you should be able to interpolate using interp3.
FinterpX = interp3(Xmeas,Ymeas,Zmeas,FmeasX,Xgrid, Ygrid, Zgrid);
FinterpY = interp3(Xmeas,Ymeas,Zmeas,FmeasY,Xgrid, Ygrid, Zgrid);
FinterpZ = interp3(Xmeas,Ymeas,Zmeas,FmeasZ,Xgrid, Ygrid, Zgrid);
However, your random datapoints may not work well with this approach, due to the constraints that interp3 puts on them.

Making a 3D plot of multiple column vectors

I have multiple vectors of varying lengths that I would like to plot next to each other in 3D space in Matlab.
As an example:
Say I have three vectors:
X is a 5x2 vector,
Y is a 10x2 vector and
Z is a 15x2 vector.
Each element of every vector has the format:
x value, y value
but the x values of the various vectors do not match.
I would like to plot these vectors in 3D space, next to each other. The reason why I don't want to plot them using "hold" is because most of the data have the same values, but I would like to see how many of the plots have the same value at a specific time.
I hope my questions makes sense. Please just ask if anyone is unsure.
I think you are looking for the function ribbon.
Documentation: http://www.mathworks.fr/help/techdoc/ref/ribbon.html
EDIT:
if your x's do not have the same length, you can combine it with interp1 as follow:
x1=0:0.1:1;
x2=0:0.02:1.5;
y1=x1.^2;
y2=sqrt(x2);
y2=interp1(x2,y2,x1);
ribbon(x1',[y1;y2]')