フィルターのクリア

1x2 Cell with each ?field? containing 2 values

3 ビュー (過去 30 日間)
Patrick Petre
Patrick Petre 2020 年 11 月 27 日
コメント済み: Ameer Hamza 2020 年 11 月 29 日
I have a Cell called "a" which is 2x1.
so from "a" to "a{1,1}" and "a{1,2}"
Each ?field? correct me pls if this is not the right term. contains 2 values.
How can i extract the first value of both fields? If i try t = a(1,1) it gives me both values but i jsut want the first one.

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 11 月 27 日
編集済み: Ameer Hamza 2020 年 11 月 27 日
You have 'a' like this
a = {[1 2], [3 4]};
To access first elemets, you need to use indexing like this
a{1,1}(1)
a{1,2}(1)
If you want to extract all the first elements, then you will need to use a loop
v = zeros(size(a));
for i = 1:numel(a)
v(i) = a{1,i}(1);
end
The above for-loop can be simplified using cellfun()
v = cellfun(@(x) x(1), a);
  4 件のコメント
Patrick Petre
Patrick Petre 2020 年 11 月 28 日
編集済み: Patrick Petre 2020 年 11 月 28 日
okay. it is a bit weird bc now it worked with my example, but not with my bar plot. it just plots me p{1,2}(1). p and xxx are basically the same with different values, so i dont understand why it doesnt work (Inside my loop i fit a curve to a set of data which is my p)
for i = 1:number (number being 2)
p{i} = polyfit(x_relevant{i}, y_relevant{i},1)
subplot(4,1,4)
b(i) = bar(p{1,i}(1),'FaceColor',c{i})
hold on
xxx = {[1 2], [3 4]}
x222(i) = xxx{1,i}(1)
end
Ameer Hamza
Ameer Hamza 2020 年 11 月 29 日
Can you attach your actual code with all the relevant variables?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by