indexing a numbering in a string.
8 views (last 30 days)
Show older comments
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 Comments
Answers (4)
Walter Roberson
on 2 Jan 2013
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 Comments
Walter Roberson
on 2 Jan 2013
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
on 7 Jan 2013
2 Comments
Walter Roberson
on 7 Jan 2013
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
on 7 Jan 2013
"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 Comments
See Also
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!