【问题标题】:Using MATLAB to stack several 2D plots generated from .csv into a 3D plot使用 MATLAB 将从 .csv 生成的多个 2D 图堆叠成 3D 图
【发布时间】:2018-01-15 21:56:29
【问题描述】:

我有代码可以根据存储在多个 .csv 文件中的数据生成二维图:

clearvars;
files = dir('*.csv');
name = 'E_1';
set(groot, 'DefaultLegendInterpreter', 'none')
set(gca,'FontSize',20)
hold on;
for file = files'
    csv = xlsread(file.name);
    [n,s,r] = xlsread(file.name);
    des_cols = {'Stress','Ext.1(Strain)'};
    colhdrs = s(2,:);
    [~,ia] = intersect(colhdrs, des_cols);
    colnrs = flipud(ia);
    file.name = n(:, colnrs);
    file.name = file.name(1:end-500,:);
    plot(file.name(:,2),file.name(:,1),'DisplayName',s{1,1});
end
ylabel({'Stress (MPa)'});
xlabel({'Strain (%)'});
title({name});
legend('show');

我想做的是修改代码,以便将由 .csv 数据制作的 2D 图连接成 3D 图,其中一个轴是 files 中的 .csv 索引,有点像图片在这个post 的顶部。我从那篇帖子中得到了使用plot3 的想法,但我不确定如何让它发挥作用。

据我了解,我需要创建 3 个新矩阵 xMat, yMat, zMat。每个矩阵的列包含来自 csv 文件的数据,yMat 包含的列只是 csv 的索引,但我不完全确定从这里去哪里。

感谢您的帮助!

【问题讨论】:

标签: matlab csv plot


【解决方案1】:

您可以在循环中调用plot3,如下所示。基本上将 Y 值更改为 Z 值。然后在循环的每次迭代中将 Y 加一。

figure;
a = axes;
grid on;
hold(a,'on');
x = 0:.1:4*pi;
for ii = 1:10    
    plot3(a,x,ones(size(x))*ii,sin(x)); 
end
view(40,40)

修改您的代码将如下所示。请注意,由于我没有您的 CSV,因此无法测试任何这些。

clearvars;
files = dir('*.csv');
name = 'E_1';
set(groot, 'DefaultLegendInterpreter', 'none')
set(gca,'FontSize',20)
a = gca;
hold on;
ii = 1;
for file = files'
    csv = xlsread(file.name);
    [n,s,r] = xlsread(file.name);
    des_cols = {'Stress','Ext.1(Strain)'};
    colhdrs = s(2,:);
    [~,ia] = intersect(colhdrs, des_cols);
    colnrs = flipud(ia);
    file.name = n(:, colnrs);
    file.name = file.name(1:end-500,:);
    plot3(a,file.name(:,2),ones(size(file.name(:,2))).*ii,file.name(:,1),'DisplayName',s{1,1});
    ii = ii+1;
end
view(40,40);
ylabel({'Stress (MPa)'});
xlabel({'Strain (%)'});
title({name});
legend('show');

【讨论】:

  • @enea19 如果这解决了您的问题,然后投票并接受答案,这样它就不会一直无人回答。
  • 我试图投票,但它说我不能,因为我没有足够的代表。我将其标记为已回答。
猜你喜欢
  • 1970-01-01
  • 2019-03-05
  • 2021-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-27
  • 1970-01-01
  • 2013-12-24
相关资源
最近更新 更多