user2850087
user2850087

Reputation: 21

Plot a Cell Array in MATLAB

I am attempting to plot a cell array of data, where I solve for a radius 'ry' based on a given theta 't'. I am using a for loop to store the data in this cell array.

for t = 0:pi/100:2*pi
    cell(n,1) = t;
    cell(n,2) = (1/4*pi)*((K1c/Sys)^2)*(1+cos(t)+(3/2)*(sin(t/2)^2));
    n=n+1; 
end;

Where K1c = 45 and Sys = 40. My issue is attempting to plot this cell.

Obviously, it is not as simple as using plot(cell), or using plot(cell(n,1),cell(n,2)). Any suggestions would be greatly appreciated.

Thanks guys,

Cody

Upvotes: 2

Views: 13052

Answers (2)

Chris Taylor
Chris Taylor

Reputation: 47392

You don't need to make it so complicated. Matlab is designed to easily handle whole vectors and matrices of data at once, without the need for loops.

t = 0: pi/100: 2*pi;
y = (pi/4) * (45/40)^2 * (1 + cos(t) + 3/2 * sin(t/2).^2);
plot(t, y)

Which results in

enter image description here

Upvotes: 1

lhcgeneva
lhcgeneva

Reputation: 1981

You are not using a cell array. The way you store your data is a normal matrix. The plot command is then

plot(cell(:, 1), cell(:, 2))

If you wanted to store your data in a cell you'd have to reassign your matrix cell to some other variable (as cell is a reserved expression in matlab)

a = cell;
clear cell;
b = cell(1, 2) %Create 1x2 cell
b{1} = a(:, 1);
b{2} = a(:, 2);
plot(b{1}, b{2});

Upvotes: 2

Related Questions