indexing a numbering in a string.
8 ビュー (過去 30 日間)
古いコメントを表示
This is two questions in one. Firstly, I am trying to add labels to an array, but for the life of me I cannot figure out how to, so I decided to use a cheaty way using the statistics tool dataset.
In the program I have indexed a variable number of loops. These loops are stored in the array, and I want to be able to number the labels on the array to display something like:
Division 1, Division 2, Division 3,.....Division n, Total Cells, Lin A, Lin B
So basically, I want the first set of labels to be able to be indexed, and then there will be a few labels that are not indexed.
The first thought was that I need a string array that was indexed. I thought I could do something like:
for i=1:n
char(i)=('Help' num2str(i));
end
but this is not working. It will to accept the indexed number and the string. together, although the code without the text works fine.
for i=1:n
char(i)=(num2str(i));
end
The second issue, is that when i try to use dataset, with an array created as the above char, it will not accept it.
B=dataset({results, char,'Lineage 1','Lineage 2','Total Cells'})
where "results" is the data that will go into the table. And "char" right now is simply ['1' '2' '3'...'n'] built as above (without the text I want). If you have any suggestions please let me know!
0 件のコメント
回答 (4 件)
Walter Roberson
2013 年 1 月 2 日
Do not name a variable "char": that overrides the char() function.
for i = 1 : n
labels{i} = sprintf('Division %d', i);
end
B = dataset({results, labels{:},'Lineage 1','Lineage 2','Total Cells'})
4 件のコメント
Walter Roberson
2013 年 1 月 2 日
How many columns are there in "results" ? How does that compare to "n" ? If the number of columns in "results" is not n+3 then you would get that error. The "+3" being for 'Linage 1', 'Linage 2', 'Total Cells'
JJ
2013 年 1 月 7 日
2 件のコメント
Walter Roberson
2013 年 1 月 7 日
Correct, dataset() does not take a vector for the column titles. Which is why the code I gave above
B = dataset({results, labels{:},'Lineage 1','Lineage 2','Total Cells'})
does not use a vector for the column titles. The construct labels{:} does not mean 'pass in the array "labels" as an array at that point': it means 'at this point create new arguments just as if you had originally typed them in as part of the call, with each successive argument taking a value from the next element of the cell array "labels"'
Did you try
B=dataset({results,label{:}});
I notice you did not answer my previous question about how many columns there were in "results" and how that compares to "n".
Jan
2013 年 1 月 7 日
"I want the first set of labels to be able to be indexed"
Then using an index seems to be obvious:
Division{1} = ...
Division{2} = ...
...
This is much more convenient than hiding the index in the name of the variable. But if you really have good reasons to do this, dynamic fieldnames are a solution also:
for ii = 1:10
data.(sprintf('Division %d', ii)) = rand(1,10);
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Text Data Preparation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!