Confused about MATLAB visualization - matlab

I have a 24 dimensional dataset of 100,000 data points which are mapped onto 2d plane using sammon mapping (a method ro reduce dimensionality and visualize). The scatter plot is shown below! Doesn't look too interesting..
But when i tilted my screen an looked at the figure, a square array of points appeared. I am not a matlab user and i am confused whether there is some meaning
to this for example are the points forming a clusters etc ? or is this just some sorcery.

Please use the scatter command and change the scatter properties.
fig = figure('Color','white');
sc = scatter(y_stacked(:,1), y_stacked(:,2));
sc.Marker = '.';
sc.SizeData = 1;
Then, make the figure window as big as possible and/or zoom in to examine your data points more closely.

Related

How to fit a surface ((x,y,z) matrix) using Matlab?

I made AFM (Atomic Force Microscopy) measurements. I exported my data from Gwyddion to a text file (can be download here), such as I load it in Matlab like:
data = importdata('001_Zsensor.xyz');
x=data(:,1);y=data(:,2);z=data(:,3);
shading('interp');
tri = delaunay(x,y);
figure(1)
tsurf=trisurf(tri,x,y,z,'EdgeColor','none','Facecolor','interp');
So now I have my surface. It corresponds to the rugosity of a bead, so I need to extract the spherical feature of this surface in order to recover the rugosity landscape over a plane surface (from which I can then compute my physical parameters).
Basically, I want to fit a ellipsoid to the surface I previously defined (tsurf). I tried using cftool (even though I'd rather use command so I can put this in a Matlab script) but as the equation should be of form
z=f(x,y)
and the ellipsoid's equation is
((x-x0)/a)^2 + ((y-y0)/b)^2 + ((z-z0)/c)^2 = 1,
I did not manage to get the fit to work.
How can I do so?
Thank you very much.
Ok, found it! Here it is:
Following the previous code lines I wrote (see the question of this post):
ffit = fit([x y],z,'poly22')
figure(1)
hold on
plot(ffit)
Of course, the fit can be more constrained if needed.

How to fill below 2d graphes in 3d with plot3()?

i plotted a 3d plot with the following code, but with my own measured data:
Data = rand(1000, 4); % 4 Channels or more or less
Time = 1:1000;
sData = size(Data);
vSignal = 1:sData(2);
plot3(Time, vSignal(ones(1, sData(1)), :), Data);
view(-150, 60)`
Now I want to fill under the graphes I plotted.But it´s really important for me to be able to plot and fill below multiple graphes!
Do I have to use fill3()? I imported my data as a numeric matrix. I tried several ways but I failed.
Thanks for help.
Here is how to use fill3:
for ii = 1:sData(2)
fill3([Time,Time(end),Time(1)],ii*ones(1,size(Time,2)+2),[Data(:,ii);zeros(2,1)],ii*ones(2+size(Time,2),1),'EdgeColor','none');
end
I use the for loop since I create a patch for each plot separately (I call the filled-in area a Patch). For each patch, I need to give its boundaries, and fill3 will fill everything in between. The boundaries are the exact points that you used in plot3, but in addition you need two more points on the x-y plane to close the patch (to make it a polygon). That is the reason I extend the vectors by 2.

Smooth Excel-like plot with correct legend in Matlab

I have some sparse data and want to plot them as markers connected by a smooth, interpolated line - like the default behaviour of Microsoft Excel.
There are solutions to this problem easily found on the internet, but I find them unsatisfactory. What they do is: plot the sparse data as one data set drawing it as markers without lines, interpolate it with a method of choice and plot the interpolation as the second data set, with lines without markers.
The problem with these tricks is that in the legend the two data sets will be listed separately. I would expect a single data set depicted in the legend as a line crossing through a marker.
Is it possible in Matlab?
If you want to plot an interpolated line there are lots of ways to do that. You can try generating an interpolated line using the matlab interp1() function.
Let's create x and y data with no NaN.
x = randn(1,10)
y = randn(1,10)
If you want 1000 data points where previously you only had a few, that's pretty easy:
x2 = min(x):(max(x)-min(x))/1000:max(x)
y2 = interp1(x,y,x2,'cubic')
and you can plot your data and spline using
plot(x,y,'r+')
hold on
plot(x2,y2,'r-')
A custom legend is straightforward when you use handle graphics. You can plot a dummy data set with a red line passing through a marker using
h(1) = plot(NaN,NaN,'r-+')
lstring{1} = 'Data';
You can then add a legend that points to this data set using
legend(h,lstring)
You'll end up with something that looks roughly like this:
The nice thing about using handle graphics (i.e. the h) is you can throw whatever data series you want into the legend as h(end+1) and lstring{end+1}.

Matlab 3d plot of indexed data

I am trying to plot a 3d view of a very large CT dataset. My data is in a 3d matrix of 2000x2000x1000 dimension. The object is surrounded by air, which is set to NaN in my matrix.
I would like to be able to see the greyscale value of the surface of the object (no isosurface) but I cannot quite work out how to do that in Matlab. Can anyone help me please?
Given that I a dealing with a huge matrix and I am only interested in the surface of the object, does anyone know a good trick how to reduce the size of my dataset?
The function surf(X,Y,Z) allows you to plot 3d data, where (X,Y) gives the coordinates in the x-y-plane while Z gives the z-coordinate and the surface color.
By default the function does not plot anything for the NaN entries, so you should be good to go with the surf function.
To set the surf-function to use a grayscale plotting use:
surf(matrix3d);
colormap(gray);
This plots the matrix in a surface plot and sets the colormap to grayscale.
In addition, as I understand your data, you might be able to eliminate entire plane-segments in your matrix. If for instance the plane A(1,1:2000,1:1000) is NaN in all entries you could eliminate all those entries (thus the entire Y,Z-plane in entry X=1). This will however require some heavy for loops, which might be over the top. This depends on how many data matrices you have compared to how many different plot you want for each matrix.
I will try to give you some ideas. I assume lack of a direct 3D "surface detector".
Since you have a 3D matrix where XY-planes are CT scan slices and each slice is an image, I would try to find edges of each slice say with edge. This would require some preprocessing like first thresholding each slice image. Then I can either use scatter3 to display the edge data as a 3D point cloud or delaunay3 to display the edge data as a surface.
I hope this will help you achieve what you are asking for.
I managed to get it working:
function [X,Y,Z,C] = extract_surface(file_name,slice_number,voxel_size)
LT = imread(file_name);%..READ THE 2D MAP
BW = im2bw(LT,1);%..THRESHOLD TO BINARY
B = bwboundaries(BW,8,'noholes');%..FIND THE OUTLINE OF THE IMAGE
X = B{1}(:,1);%..EXTRACT X AND Y COORDINATES
Y = B{1}(:,2);
indices = sub2ind(size(LT),X,Y);%..FIND THE CORRESPONDING LINEAR INDICES
C = LT(indices);%..NOW READ THE VALUES AT THE OUTLINE POSITION
Z = ones(size(X))*slice_number;
I can then plot this with
figure
scatter3(X,Y,Z,2,C)
Now the only thing I could improve is to have all these points in the scatter plot connected with a surface. #upperBound you suggested delaunay3 for this purpose - I cannot quite figure out how to do this. Do you have a tip?

matlab - can I use roipoly to get data from a scatter plot?

I want to select data using a polygonal shape. I understand roipoly does that for 'images'. is there something like this for scatter plots?
You can use data brushing to mark data on a scatter plot then extract it to the workspace. Look for the little brush symbol at the top of a figure window.
See Marking up graphs with Data Brushing from Matlab, and Accessing plot brushed data from the very useful Undocumented Matlab.
If you want to draw a complex polygon, you can use impoly and inpoly:
X = rand(200, 2);
scatter(X(:,1), X(:,2));
h = impoly();
% now you go and position the polygon, control returns once you've 'finsished' with it '
nodes = getPosition(h);
selected_indices = inpoly(X, nodes);