indexing a numbering in a string.

8 ビュー (過去 30 日間)
JJ
JJ 2013 年 1 月 2 日
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!

回答 (4 件)

Walter Roberson
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 件のコメント
JJ
JJ 2013 年 1 月 2 日
The error I am getting is:
??? Error using ==> dataset.dataset>dataset.dataset at 129
Must have one variable name for each column when creating multiple variables from an array.
Walter Roberson
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
JJ 2013 年 1 月 7 日
the function 'dataset' does not seem to want to accept a vector for the column titles. even if i write a program as follows;
results=ones(1,6);
for i=1:6
label{i} = transpose(sprintf('Division %d', i)); %had to transpose it because otherwise this becomes a string column vector.
end
B=dataset({results,label});
Any thoughts anyone? Is there another better way to title these columns that will accept a string vector?
  2 件のコメント
Walter Roberson
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
Jan 2013 年 1 月 7 日
編集済み: Jan 2013 年 1 月 7 日
Have you tried your code? The transposition is not useful here, because sprintf does not create a column vector, but as expected a row vector.

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


Jan
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

JJ
JJ 2013 年 1 月 7 日
thanks Walter! You were right, I was not using the right bracketing. Sorry! I had to read up about the bracketing.
Jan, thanks! something like that is also working for me!

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by