How to extract data from a cell array?

85 次查看(过去 30 天)
Hello, it's the first time that i use arrays and I have a series of 3x1 cell arrays, each of them has 3 cells vith a differet numbers of numerical data. I want to use for loop or while loop in order to ectract the three cells and convert them into different 3 vectors:
C={[2.24;2.28;2.31], [0.99;1.44;1.44;1.44;1.44;2.12;2.25;2.48;3.61], [1.86;1.89;1.89;1.89;1.89;1.89;1.89]};
for j=1:length(C)
x(j)=C{j};
end
at the end I should have:
x1=[2.24;2.28;2.31]
x2=[0.99;1.44;1.44;1.44;1.44;2.12;2.25;2.48;3.61]
x3= [1.86;1.89;1.89;1.89;1.89;1.89;1.89]
  1 个评论
Stephen23
Stephen23 2020-1-6
"..at the end I need to have: x1... x2...x3"
Is there a particular reason why you cannot just use indexing to access your data?
Using numbered variables is certainly possible, but it will force you into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
Indexing is simple, neat, easy to debug, and very effiicent. Unlike what you are trying to do.

请先登录,再进行评论。

采纳的回答

KSSV
KSSV 2020-1-6
Already you have the cells in your desired way.
You can access them using C{1} , C{2}, C{3} .
You need to store them again into X1, X2, X3.
  3 个评论
KSSV
KSSV 2020-1-6
There is no requirement to do this.
Guillaume
Guillaume 2020-1-6
"but i need to convert my array"
Why? As Stephen pointed out in his comment to your question, this will complicate your code for no useful benefit as far as we can tell. So please explain this need.
Why can't you write C{1} wherever you were going to write X1?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by