MATLAB-saving data after loops for surf plot

In summary, the user is trying to plot a function on a surface, but is having trouble because of the odd function. They are using a nested for loop to iterate through the data, but they are not sure how to correctly save the results. They are getting an error when trying to plot the results.
  • #1
RicciFlow
15
0
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:
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! :)
 
Physics news on Phys.org
  • #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?
 
  • #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 don't know if this was what you wanted although.
 
  • #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.
 
  • #5


Hi there,

It looks like the issue you are facing is with concatenating the values to the Z matrix. When using the square brackets [ ] to concatenate, the dimensions of the matrices being concatenated must match. However, in your code, the dimensions of Z (a scalar value) and z (a 1x1 matrix) do not match, hence the error.

To fix this, you can use the "end" keyword to add elements to the end of the Z matrix in each iteration. Your code would look something like this:

clear
clc
Z=[]; %initialize Z as an empty matrix
for x=-10:.5:10
for t=-10:.5:10
z=Om(x, t);
Z(end+1,end+1)=z; %add z to the end of Z in each iteration
end
end
figure;
surf(Z);

Hope this helps!
 

1. How do I save data after a loop in MATLAB for a surf plot?

To save data after a loop in MATLAB for a surf plot, you can use the "save" function. This function allows you to save variables and arrays to a specified file name. You can also use the "xlswrite" function to save data to an Excel file.

2. What is the best way to organize data for a surf plot in MATLAB?

The best way to organize data for a surf plot in MATLAB is to use matrices. A matrix is a two-dimensional array where each element represents a point on the plot. By organizing your data into a matrix, you can easily plot it using the "surf" function.

3. Can I save my surf plot as an image in MATLAB?

Yes, you can save your surf plot as an image in MATLAB by using the "saveas" function. This function allows you to save your plot in various formats such as PNG, JPEG, and PDF. You can also use the "print" function to save your plot as an image.

4. How do I change the colors and labels in a surf plot in MATLAB?

To change the colors in a surf plot, you can use the "colormap" function. This function allows you to specify a color scheme for your plot. To add labels to your plot, you can use the "xlabel", "ylabel", and "zlabel" functions to specify the labels for the x, y, and z axes, respectively.

5. Is it possible to add a legend to a surf plot in MATLAB?

Yes, you can add a legend to a surf plot in MATLAB by using the "legend" function. This function allows you to specify the labels for different elements in your plot, such as different data sets or lines. You can also customize the position and appearance of the legend using additional parameters in the "legend" function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
762
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top