How to plot a 3D surface in matlab using set of data [duplicate] - matlab

This question already has answers here:
How to create a 2D-matrix out of my data for surf()?
(2 answers)
Closed 8 years ago.
I have a set of 3D data which are not ordered. I need to plot a surface which basically connects them together and color the surface based on my z values.
Here is an example of the data that I have.
-0.144847 -5.239271 -0.000000
-5.430672 -0.044747 0.000000
0.006860 0.282666 -0.000000
0.385219 0.005522 -0.000000
-6.464983 -7.105215 -0.000000
-7.028026 -4.026576 0.000000
-6.092855 -1.826723 -0.000000
-5.619010 -0.886051 0.000000
-5.769190 -0.487232 0.000000
-3.496163 -7.561789 -0.000000
-1.883844 -7.683578 0.000000
-0.845860 -6.159235 -0.000000
-0.487085 -5.865159 0.000000
-5.442694 0.346838 -0.000000
0.158343 -5.402572 -0.000000
0.075667 0.309257 0.000000
0.648079 -1.368195 0.000000
0.510721 -0.525318 0.000000
-0.749283 0.435931 0.000000
-0.338834 0.399974 -0.000000
-0.151157 0.367955 0.000000
-0.071820 0.371475 0.000000
-0.125133 -4.846842 -1.025075
-5.425106 0.058539 -0.341047
0.006806 0.282034 -0.057553
0.384929 0.005456 -0.021184
-5.890619 -6.417012 -0.339540
-6.828500 -3.837339 -0.394468
-6.048110 -1.732177 -0.362965
-5.597372 -0.822289 -0.333655
-5.815376 -0.394711 -0.360211
-3.447449 -7.345610 -0.198281
-1.883239 -7.680056 -0.098589
-0.845817 -6.153723 -0.041199
-0.487017 -5.863854 -0.019289
-5.475775 0.419626 -0.104516
0.159202 -5.400629 -0.017949
0.189255 0.205864 -0.010534
0.280893 0.151170 -0.015441
0.327987 0.090052 -0.018171
0.128256 0.272671 -0.006972
0.075667 0.309256 -0.003940
0.652881 -2.917007 -0.040059
0.647424 -1.368548 -0.037539
0.510386 -0.525737 -0.029055
0.546600 -0.273572 -0.030561
0.448142 -0.108028 -0.024821
-1.643195 0.460307 -0.102182
-0.751135 0.435615 -0.045282
-0.326989 0.388574 -0.019874
-0.151168 0.367766 -0.009394
-0.071815 0.371646 -0.004734
-0.125133 -4.846842 -1.025075
-5.425106 0.058539 -0.341047
0.006806 0.282034 -0.057553
0.384929 0.005456 -0.021184
-5.890619 -6.417012 -0.339540
-6.828500 -3.837339 -0.394468
-6.048110 -1.732177 -0.362965
-5.597372 -0.822289 -0.333655
-5.815376 -0.394711 -0.360211
-3.447449 -7.345610 -0.198281
-1.883239 -7.680056 -0.098589
I have around 400 points, like the ones above.
Thank you in advance for your help.

What format is your data in and what kind of plot do you want? Once you have separated the data into 3 variables it is simple to plot it into a 3-D scatter plot and specify the color using:
scatter3(X,Y,Z,S,C)
More info is available on the mathworks website which is SUPER useful for figuring out MATLAB

Related

Export/Rasterize alpha shape to bitmap

I build an alpha shape from some points (example given in code) and want to export the shape to a raster graphics format. I need the shape only, not the plot markings (axis, scales ect).
I need only the resulting triangle on white ground as a bitmap.
Scale needs to be 1 unit = 1 pixel.
x = [0 10 20 30 30 30 15];
y = [0 0 0 0 15 30 15];
shape = alphaShape (x',y');
plot (shape, 'FaceColor', 'black');
I have not found anything on how to export shapes or how to rasterize them. Is there any way to do that?
Run the following code after yours.
imgwidth = max(1, ceil(max(x) - min(x)));
imgheight = max(1, ceil(max(y) - min(y)));
ax = gca;
ax.Visible = 'off';
ax.XTickMode = 'manual';
ax.YTickMode = 'manual';
ax.ZTickMode = 'manual';
ax.XLimMode = 'manual';
ax.YLimMode = 'manual';
ax.ZLimMode = 'manual';
ax.Position = ax.OuterPosition;
af = gcf;
figpos = getpixelposition(af);
resolution=get(0, 'ScreenPixelsPerInch');
set(af, 'paperunits','inches', ....
'papersize',[imgwidth imgheight]/resolution, ....
'paperposition',[0 0 [imgwidth imgheight]/resolution]);
print(af,'out.png','-dpng',['-r',num2str(resolution)],'-opengl')
Things done:
Fetch data range and convert to image dimensions.
Turn off axes and ticks.
Minimize/remove padding space surrounding the actual content.
Map 1 unit in data into 1 pixel in output image.
Things not done:
Guarantee aspect ratio. (should work, though)
This screenshot shows non-unity aspect ratio output:
References
Mathworks - Save Figure at Specific Size and Resolution
MATLAB Central - saving a figure at a set resolution
Mathworks - print
Mathworks - Save Figure with Minimal White Space

Convert SIint16 to float on iPhone4

I am developing an app which uses simple pitch perception and it runs fine in the Xcode Simulator. The app loads in the iPhone 4 and I can navigate the app but no output is shown . I have started to debug and find that when I convert Sint16 to float I get an overflow error.
I have tried vDSP and a simple loop.
simplified code n=1536
sampleBuffer = (SInt16*) malloc(n);
floatSamples = (float*) malloc(sizeof(float)*n);
// Convert SInt 16 to float
for(int i = 0; i<n; i++) {
floatSamples[i] = (float)samples[i];
}
//vDSP_vflt16(samples,1,floatSamples,1,n);
This results in
-0.000000
-0.000000
-0.000000
964957789008661674961361960960.000000
-5629971079108504200413184.000000
-inf
35116851871418647838720.000000
-inf
0.000000
0.000000
0.000000
-1233.760620
288987875649316726325339192557568.000000
-0.000000
-0.000000
-0.000000
-7508525217561044282816045485425426432.000000
-656399043038732589927376093184.000000
0.000000
-0.000053
9470091451011432448.000000
-24555002
similar result from vDSP
Everything is fine in the simulator on iPhone 4s all the vDSP calculations work.
iPhone is running ios7.1.2 and xCode 6 on MacBook pro.
Just looking for a clue really.
The code
sampleBuffer = (SInt16*) malloc(n);
allocates n bytes, not n SInt16 values.
You need:
sampleBuffer = (SInt16*) malloc(sizeof(SInt16)*n);

ShinobiChart ignoring series made of 0s

I'm using ShinobiControls for creating a simple chart. When the dataset is:
#{
#"Value": #0.0,
#"Date": #1364828400
},
#{
#"Value": #0.0,
#"Date": #1364914800
},
#{
#"Value": #0.0,
#"Date": #1365001200
}
]
I receive this error message:
Ignoring range with 0.000000 span
From: SChartNumberAxis at 0xc287030, axisRange = { 0.000000, 0.000000 }, defaultRange = { 0.000000, 0.000000 }, maxRange = { 0.000000, 0.000000 }
and my app gets stuck. No error message. Nothing.
Has anybody seen anything similar?
It seems to me that you have not set range for the axis tied to the Value dimension of your points. Supposing that value is displayed on the Y axis. Try:
SChartNumberRange *yAxisRange = [[SChartNumberRange alloc] initWithMinimum:[NSNumber numberWithInt:0] andMaximum:[NSNumber numberWithInt:10]];
SChartNumberAxis *yAxis = [[SChartNumberAxis alloc] initWithRange:yAxisRange];

iPhone OpenGL ES1 VBO Texture Coordinate Issues

I switched my code to use VBO and suddenly my sprites (2 triangle triangle-strip) have bad texture coordinates.
Disabling VBO makes the problem go away.
Note - I align my vertex and texture coordinate data to 4 bytes as specified in this link:
https://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/TechniquesforWorkingwithVertexData/TechniquesforWorkingwithVertexData.html
This is a printout of the vertex data, it goes:
x, y, z, 0.0f, tu, tv, 0.0f, 0.0f
Vertex 0 -0.500000 0.500000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000
Vertex 1 -0.500000 -0.500000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
Vertex 2 0.500000 0.500000 0.000000 0.000000 1.000000 1.000000 0.000000 0.000000
Vertex 3 0.500000 -0.500000 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000
This is how I generate the VBOs:
m_numVertices = 4
m_stride = 8
glGenBuffers( 1, &m_vboVertices); // Generate our Vertex Buffer Object
glBindBuffer( GL_ARRAY_BUFFER, m_vboVertices); // Bind our Vertex Buffer Object
glBufferData( GL_ARRAY_BUFFER, (m_numVertices * m_stride) * sizeof(float), m_pVertexData, GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW
glBindBuffer( GL_ARRAY_BUFFER, 0 );
glGenBuffers( 1, &m_vboIndices );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_vboIndices );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte)*4, m_pIndices, GL_STATIC_DRAW );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
And this is how I render the sprite:
glBindBuffer(GL_ARRAY_BUFFER, m_vboVertices);
glVertexPointer( 3, GL_FLOAT, byteStride, BUFFER_OFFSET(0) );
glTexCoordPointer( 2, GL_FLOAT, byteStride, BUFFER_OFFSET(4) );
glBindBuffer(GL_ARRAY_BUFFER,0); // reset
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vboIndices );
glDrawElements(GL_TRIANGLE_STRIP, m_numVertices, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0); // reset
BUFFER_OFFSET is defined as:
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
With VBO:
Without VBO:
Thanks.
Edit - After messing around a bit with the code, it appears that this problem also occurs when drawing using GL_TRIANGLES.
It appears that the problem is that I am not calculating the glTexCoordPointer offset in bytes. It should be:
glTexCoordPointer( 2, GL_FLOAT, byteStride, (char*)NULL + (4*sizeof(float)) );
The Macro BUFFER_OFFSET was incorrect.
Applying this fix solved the problem.

The Gimp - Creating background gradient

I have the below css code for a web gradient on my page, I would like to make a background image that is exact to this gradient using the Gimp. Anyone have expertise doing this that might be able to lend some advice? Thanks
background-image:-webkit-linear-gradient(90deg, rgba(51, 51, 51, 1.00) 0.0% , rgba(26, 26, 26, 1.00) 50.5% , rgba(51, 51, 51, 1.00) 50.7% , rgba(77, 77, 77, 1.00) 100.0% );
GIMP can't parse that directly, althoug GIMP 2.8 ships with a Python script that can output gradients in this CSS syntax
You could make a python-script to parse CSS gradient syntax into GIMP Gradients,
and them use this gradient on an image.
Of course it is overkill if you are needing that just once -
I'd recommend creating a new gradient in GIMP, and manually edit the recorded file
(in ~/.gimp-2.8/gradients folder if you are on *nix, else check for the user gradients folder in the preferences).
GIMP's gradient file is straightforward - a text only file that goes like:
GIMP Gradient
Name: Untitled
2
0.000000 0.243464 0.486928 0.000000 0.000000 0.000000 1.000000 0.000000 1.000000 0.000000 1.000000 0 0 0 0
0.486928 0.743464 1.000000 0.000000 0.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 0 0 0 0
So this is a single gradient, with two segments - each line has the start-point, endpoint of each segment, the starting ARGB color, ending ARGB color, and ,...don't care, just keep the four zeros at the end: most likely they are used to describe the type of color in each endpoint, and we want 0.
Those rgba colors correspond to the following html-notations :
rgba(51, 51, 51, 1.00) - #333333 (Color A)
rgba(26, 26, 26, 1.00) - #1a1a1a (Color B)
rgba(51, 51, 51, 1.00) - #333333 (Color A)
rgba(77, 77, 77, 1.00) - #4d4d4d (Color C)
You could try creating a rectangular image (with height twice the width). Fill the top square half with a gradient of color A to color B, and the bottom square with a gradient from color A to color C.
Then you set it as your background image with "repeat" property enabled.