indexing a numbering in a string.

8 views (last 30 days)
JJ
JJ on 2 Jan 2013
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!

Answers (4)

Walter Roberson
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
JJ
JJ on 2 Jan 2013
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 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'

Sign in to comment.


JJ
JJ on 7 Jan 2013
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 Comments
Walter Roberson
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
Jan on 7 Jan 2013
Edited: Jan on 7 Jan 2013
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.

Sign in to comment.


Jan
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

JJ
JJ on 7 Jan 2013
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!

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!