在MATLAB中将plot3转换为冲浪 [英] Turning a plot3 into a surf in MATLAB

查看:84
本文介绍了在MATLAB中将plot3转换为冲浪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个csv文件,我使用plot3绘制了它们,以创建以下图像:

I have several csv files and I plotted them using plot3 to create the following image:

现在,我想将其转换为表面图,因为我想根据高度为图上色.我使用scatter3进行了以下操作:

Now I would like to turn this into a surface plot because I would like to colour the plot according to height. I made the following with scatter3:

clearvars;
files = dir('*.csv');
name = '\epsilon_{y} over time for Vertical section';
des_col_1 = 'Vertical section.epsY []';
des_col_2 = 'Length [mm]';
set(gca,'FontSize',20)
a = gca;
ii = 1;
x_data = [];
y_data = [];
z_data = [];
tStart = tic;
for file = files'
    csv = xlsread(file.name);
    [n,s,r] = xlsread(file.name);
    des_cols = {des_col_1,des_col_2};
    colhdrs = s(1,:);
    [~,ia] = intersect(colhdrs, des_cols);
    colnrs = flipud(ia);
    file.name = n(:,colnrs);
    file.name = file.name(1:end,:);
    x_data = [x_data; file.name(:,2)];
    y_data = [y_data; ones(size(file.name(:,2))).*ii];
    z_data = [z_data; file.name(:,1)];
    ii = ii+1;
end
tEnd = toc(tStart);
fprintf('%d minutes and %f seconds\n',floor(tEnd/60),rem(tEnd,60));
view(40,40);
zlabel({'True strain (%)'});
xlabel({'Length along sample (mm)'});
ylabel({'Stage'});
title({name});
scatter3(a,x_data,y_data,z_data,10,z_data);
colormap(jet); %# or other colormap

这给了我

这是用比第一个数据少的一组数据进行测试的.它几乎可以满足我的要求,但是我想知道是否有一种方法可以根据我的所有数据生成真正的3D表面.我可以为所有点创建一个具有x,y和z值的矩阵,并尝试将scatter3(a,x_data,y_data,z_data,10,z_data);替换为

That was made with a smaller set of data than the first one as a test. It does almost what I want but I was wondering if there was a way to generate a true 3D surface from all my data. I can create a matrix with x, y, and z values for all points and I tried to replace scatter3(a,x_data,y_data,z_data,10,z_data); with

[X,Y] = meshgrid(x_data,y_data);
f = scatteredInterpolant(x_data,y_data,z_data);
Z = f(X,Y);
surf(a,X,Y,Z);

但是我得到的情节看起来不太好

but the plot that I get does not look very good

我很确定插值有问题,但是我对曲面不是很好,所以我不知道如何校正它.

I'm pretty sure there's something wrong with the interpolation but I'm not very good with surfaces so I don't know how to correct it.

推荐答案

surf给您带来错误的原因是您正在创建长的nx1数组,其中n =每个文件的点数乘以文件数.对于x,y,& z_data,您需要将它们放入矩阵.因此,请尝试以下更改:

The reason surf is giving you the error is you are creating long nx1 arrays Where n = number of points per file times number of files. For x,y,& z_data and you need them into a matrix instead. So try the following changes:

for file = files'
    <snipped out for length>
    x_data = [x_data; file.name(:,2).'];
    y_data = [y_data; ones(1,numel(file.name(:,2))).*ii];
    z_data = [z_data; file.name(:,1).'];
    ii = ii+1;
end

这应该使x,y和z_data的大小为nxm(n =文件数,m =每个文件点数).

This should make x, y, and z_data the size nxm (n = number of files, m = number points per file).

那你应该就能做到

surf(x_data,y_data,z_data)

这篇关于在MATLAB中将plot3转换为冲浪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆