4
votes

I have a matrix of values of type single that I want to plot as a surface. When I attempt to use the surf function in MATLAB, I get an error specifying I need to use uint8 or double instead:

x=peaks; %# Initialize x to a matrix of doubles
surf(x); %# No problem when x is of type double

Now I'll try singles:

x=single(peaks);
surf(x); 

Gives the following error:

Warning: CData must be double or uint8. 
Warning: CData must be double or uint8. 

Well that's unfortunate. I guess I'll have to increase to double precision for the colormap:

x=single(peaks);
surf(x,double(x));

Works just fine. But just for kicks let's try uint8 as well:

x=single(peaks);
surf(x,uint8(x));

Yields the following:

Warning: CData must be double or single unless it is used only as a texture data 
Warning: CData must be double or single unless it is used only as a texture data 

What the heck MATLAB? Make up your mind! So why is it that I have to use extra memory in the form of double precision to denote the colormap for the surf function? Even when MATLAB error text tells me I can use uint8 or single, depending on whichever one I didn't use?

1

1 Answers

1
votes

Love the question.

Not sure if you saw this or not, but it at least addresses your same disgust. Sounds like the Michael was disappointed with the performance in the uint8 algorithm in that he describes that it appears to create more computational work for itself with a plot that doesn't meet his aesthetic needs. I tried it with the peaks sample and this is what I get:

Raw Peaks Data

Then I added an offset to get all the plot.

Offset Peaks Data

Eh, it's fine I guess. Here's the code, hope this has been useful.

% Test code from Matlab Central
a=256*rand(5);
b=uint8(a);
figure;
surf(b,'facecolor','texturemap')

% get the example peaks data
% and plot without any scaling
x = peaks;
figure;
surf(uint8(x),'facecolor','texturemap')

% get the offset to keep all the data positive
% not pretty but functional
xp = x-min(min(x))+1;
figure;
surf(uint8(xp),'facecolor','texturemap')