Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB-saving data after loops for surf plot

  1. Apr 14, 2012 #1
    Hi everyone, it has been a while since I used MATLAB, and I find myself in need of something that I hope is rather simple. I am making a surface plot of a function that is a little odd. To plot said function I would normally use meshgrid or something, but that will not work thanks to my function.
    So instead I am running it through a nested for loop and I want to just store the data after each iteration into a matrix that I can plot using surf. But I cannot seem to remember how to correctly do this.
    Here's my code.

    Code (Text):
    clear
    clc
    Z=0;
    for x=-10:.5:10;
        for t=-10:.5:10;
            z=Om(x, t);
            Z=[Z z];
        end
    end
    figure;
    surf(Z);
    I imagine I am doing something very sill here but I am not sure. I get a horzcat error at Z=[Z z]. Thanks in advance! :)
     
  2. jcsd
  3. Apr 15, 2012 #2
    Im not sure what Om is, but to save results into a matrix after each loop you should use the matrix notation eg z(:,1), z(t), z(1,t) etc depending on what you want to save.

    What are you doing in the Om and [Z z] bits?
     
  4. Apr 15, 2012 #3
    clear
    clc
    Z=0;
    for i=1:41;
    x(i)=-10.5+(0.5*i);
    for j=1:41;
    t(j)=-10.5+(0.5*j);
    z(i,j)=x(i)*t(j);

    end
    end
    %Z(i,j)=[Z,z];
    disp(z)
    figure;
    surf(z);

    I was just playing around with your code and i could get this.
    I dont know if this was what you wanted although.
     
  5. Apr 17, 2012 #4
    The problem here is that your variable Z is a vector, not a matrix. The easiest way to do what you need to do, I think, would be to generate a matrix of (x,t) values in advance, and then pass the whole matrix (component-wise) into the function.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: MATLAB-saving data after loops for surf plot
Loading...