How to get several values from a cell array through a loop?

3 ビュー (過去 30 日間)
Phil_911
Phil_911 2020 年 7 月 1 日
コメント済み: Phil_911 2020 年 7 月 3 日
Hi there,
I'm quite new to MATLAB and thats why I need some help.
I have got a cell array of size m x 3 and I want to extract the values in all 3 columns, by identifying the right rows with the value in column 1.
e.g.
A = {'company name', date, number
'company name 2', date, number}
I also wants to use a loop because the number of companies and thus the number of rows change.
Thanks in advance

回答 (1 件)

Jyotirmay Mishra
Jyotirmay Mishra 2020 年 7 月 1 日
For example you have a cell array a = {'abc', 1, 2; 'ba', 2, 4}
You can do the following steps to find all the column of row with company name 'abc'
t = string(a(:,1)) %%This gives first column of all rows in string form
Y = "abc" == t %% This in this case will give a 2X1 array with 1 in case the name matches and 0 if it doesn't
Result = a(Y, :) %% This will give all the columns of rows with matched condition
  5 件のコメント
Jyotirmay Mishra
Jyotirmay Mishra 2020 年 7 月 2 日
Yes you can use a loop. For example- If you have a different cell array with company names and you want to check for each company name in that cell array you can do something like this
comp_name = {...} %%company name cell array
for i=1:length(comp_name)
t = string(a(:,1))
Y = string(comp_name(i)) == t
Result = a(Y, :) %% a is the name of original cell array with 3 columns
%%Result will give the required rows for each company name in comp_name cell array
end
You can also do it without converting the elements into a string
Look at this post for more insight
Phil_911
Phil_911 2020 年 7 月 3 日
But if I use your equation the result only gives me back the rows of the one company. And I need the rows of each company seperated. With your code I only receive the rows of comapany a.
And Y only has the ones for one in, my case the last company. I am missing all other companies.
Could you please give me further info on that?
Thanks a lot.

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

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by