MATLAB-saving data after loops for surf plot

  • Context: MATLAB 
  • Thread starter Thread starter RicciFlow
  • Start date Start date
  • Tags Tags
    Data Loops Plot
Click For Summary

Discussion Overview

The discussion revolves around saving data in MATLAB after iterations in nested loops for the purpose of creating a surface plot using the surf function. Participants explore various approaches to store computed values in a matrix format suitable for plotting.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant expresses difficulty in storing data after each iteration of nested loops and encounters a horizontal concatenation error with the current approach.
  • Another participant suggests using matrix notation to save results, questioning the definitions of the variables used in the original code.
  • A different participant provides an alternative code snippet that initializes matrices for x and t values, calculating z as a product of these values, but is uncertain if this meets the original intent.
  • One participant points out that the variable Z is a vector rather than a matrix and proposes generating a matrix of (x,t) values beforehand to facilitate the function's input.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to store data for the surface plot, with multiple competing views and methods presented without resolution.

Contextual Notes

Some limitations include the unclear definition of the function Om and the specific requirements for the output matrix format. The discussion also reflects varying interpretations of how to correctly structure the data storage within MATLAB.

Who May Find This Useful

Individuals working with MATLAB who are interested in data storage techniques for plotting, particularly those dealing with nested loops and surface plots.

RicciFlow
Messages
15
Reaction score
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
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?
 
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.
 
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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K