Create && Fill array variables resulting from the concatenation of string characters and numbers

1 回表示 (過去 30 日間)
Isma
Isma 2015 年 8 月 19 日
編集済み: Stephen23 2019 年 6 月 19 日
Hi
I have been unsucessfully looking for an advice on a code that loops through a dataset (of cell type) and extracting each column as data vector, and hope MATLAB pundits could assist owing to the challenging feature of this task.
[i,j]=size(testdata);
k=2;
while k<=j % looping through columns
['_' num2str(k-1)'yr']=cell2mat(testdata(:,k)); %extract data from each column in "testdata "and assign it to variables "_kyr" as column vector
k=k+1;
end
--------------------------
Error: An array for multiple LHS assignment cannot contain LEX_TS_STRING.
Thanks in advance and regards
  1 件のコメント
Stephen23
Stephen23 2015 年 8 月 19 日
You are trying to create variable names dynamically. This is a bad idea, read the answers to know why.

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

回答 (1 件)

Stephen23
Stephen23 2015 年 8 月 19 日
編集済み: Stephen23 2019 年 6 月 19 日
  3 件のコメント
Stephen23
Stephen23 2015 年 8 月 19 日
編集済み: Stephen23 2015 年 8 月 19 日
Just use cell2mat on your whole cell array, you do not need to use separate variables. Then use indexing to quickly access the rows/columns that you require.
The links that I gave will not create "additional confusion", because they all state that creating variables names dynamically is slow, buggy and a bad idea. It is "recurrent topic" because beginners keep thinking that it is a great idea.
Isma
Isma 2015 年 8 月 19 日
Thanks. This was my idea of last resort: apply cell2mat on whole array before carrying on, but will keep going through the links to see how bad 'eval' really is to the extent that it appeared to be the solution to my data cleansing. Cheers
Thanks for the links

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

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by