1

I have data sets stored to several variables like: p1_5, p1_7,p1_9....p1_19, and I want to calculate the std() of each data set. Now how to do it in a for loop in matlab? How to concatenate 'p1_' to n, but still keep it as a variable but not string?

for n = 5:2:19
    std(p1_??);  
end
1
  • 4
    I would highly recommend restructuring your data so you do not have to use an eval command as Dan had recommended in his answer. Commented Nov 25, 2013 at 12:42

2 Answers 2

3

You can use eval for this:

for n = 5:2:19
    eval(['std(p1_', num2str(n), ')']);  
end

But you should probably consider restructuring your code to not have to. Could you store all your p1s in a 3D matrix or a cell array?

Sign up to request clarification or add additional context in comments.

Comments

2

You could put them in a cell array. Even better, if they have the same dimensions, stack them in a matrix.

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.