フィルターのクリア

How to use cell2mat in a for loop?

1 回表示 (過去 30 日間)
Harsha M V
Harsha M V 2018 年 6 月 9 日
コメント済み: Harsha M V 2018 年 6 月 12 日
I have a column of data imported from a text file.
allcomponents =
Columns 1 through 11
'M1' 'M2' 'M3' 'M4' 'M5' 'M6' 'M7' 'M8' 'VDD' 'V+' 'VSS'
Columns 12 through 14
'Ib' 'Cc' 'CL'
and I have tried cell2mat in for loop as:
1.
for i = 1:NoofComponents
b = cell2mat(Component(i))
end
which gives separate b values
2.
for i = 1:NoofComponents
b(i) = cell2mat(Component(i))
end
error: In an assignment A(:) = B, the number of elements in A and B must be the same.
3.
for i = 1:NoofComponents
b(i,:) = cell2mat(Component(i,:))
end
error: Subscripted assignment dimension mismatch.
How do I over come this issue?
Am I doing wrong?
I want result b as
M1
M2
M3
M4
M5
M6...
  4 件のコメント
Stephen23
Stephen23 2018 年 6 月 12 日
"I want result b as..."
M1
M2
M3
M4
M5
M6...
Can you please clarify exactly what you want, because your example is not clear. Do you want to display those values in a list in the command window, or create one char array containing those values, or something else?
Harsha M V
Harsha M V 2018 年 6 月 12 日
Thank You, Sir, I got what I was looking for...

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

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 6 月 12 日
allcomponents(:)
This would be the cell array {'M1'; 'M2'; 'M3'; 'M4' ... }
If you are wanting the N x 2 character array ['M1'; 'M2'; 'M3'; 'M4' ... ]
then
char(allcomponents(:))
if the entries are not all the same size, the shorter ones will be blank padded.
But I have to wonder if what you are asking for is to take the character vectors 'M1', 'M2', and so on, and to put the contents of variables with the corresponding name into a data structure, as if you had variables M1, M2, etc, and had entered
[M1; M2; M3; ...]
?? If so then we do not recommend doing that.
I wonder if you should be using readtable() to read in your data?
Or if perhaps what you should be doing is skipping the first line of your input (the one with the labels) and reading in the rest as numeric, and then using (:) to convert to a single long vector?
  1 件のコメント
Harsha M V
Harsha M V 2018 年 6 月 12 日
Thank You,
char(allcomponents(:)) solved my problem.

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by