2

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

3
  • can you convert the cell array to a numerical array? use function cell2mat mathworks.com/help/matlab/ref/cell2mat.html and plot the matrix. Commented Oct 5, 2013 at 18:01
  • and do you have to use cell array for this? Commented Oct 5, 2013 at 18:01
  • You neither need cell nor for loops. Its a 3 liner code including "plotting". Here's a hint... define t as 0:pi/100:2*pi and proceed. Just use simple variables to store output. Commented Oct 5, 2013 at 18:11

2 Answers 2

2

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});
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.