Populate cell array dynamically

I have data output into a text file from which I can determine, for example, the following:
ssnname =
3×1 cell array
{'D1S02'}
{'D3S11'}
{'D3S12'}
ssnend =
10
20
24
What ssnname means is that the user has selected session2 from day1, session11 from day3, etc, and exported all the runs from those sessions into the text file, which I am analysing with MATLAB. What ssnend means is that after a readtable import of the text file, D1S02 data occupies the first 10 rows, D13S11 the next 10 rows, and D3S12 the final 4 rows, for a total of 24 runs exported by the user. What I'm trying to do is create a new nx1 (in this case, 24x1) cell array that contains 'D1S02' in the the first 10 values, 'D3S11' in the next 10, and 'D3S12' in the remaining 4, so that I can concatenate it into the table for plotting purposes.
This is done easily enough for a single case, but I'm trying to write code that will do it dynamically, regardless of how many sessions the user exports. As a point of info, ssnname and ssnend will always have the same number of values (rows), but the sessions themselves could contain any number of runs.
Vectorized or not, I could really use some help on this one. Thank you!

 Accepted Answer

ssnname = {'D1S02';'D3S11';'D3S12'}
ssnend = [10 20 24]
ssnend = num2cell([ssnend(1) diff(ssnend)])
output = cellfun(@(x,y) repmat(x,y,1), ssnname,ssnend','un',0);

3 Comments

I think this is going to work, although there's a bug in output as it's written above. The resulting array has repmat duplicating each character of ssnname, rather than each term. But yes this has pointed me in the right direction for certain.
What bug? The output of
output{:}
has 24 elements, which contains the repetitions you specified in your first post.
Nevermind I can see that I need to use proper indexing when retrieving the output. I am a relatively inexperienced, which is why I came to the experts. This works, and thank you very much.

Sign in to comment.

More Answers (1)

You say that after opening up one file, in a loop I guess, you "exported all the runs from those sessions into the text file" so just put them into a cell array at that point.
ca{cellIndex} = allRowsFromThisFile;

4 Comments

I do appreciate the suggestion, but exporting the sessions one by one to individual text files is what we're trying to avoid by doing the analysis with MATLAB. The text file is colon delimited and I'm using readtable('eg.text') to import the data. In this example, the table has a variable (column) which contains the following string {'D1S02, D3S11, D3S12'} repeated 24 times. So the export capability of the 3rd party application will note the names of the sessions, and each session has a marker that I can identify and which signals the end of a session in the table, but all runs (rows in the table) in a given data dump carry a string in a particular variable that contains the name of all the sessions, with each session named exactly once in the string. I'm trying to parse that string out into a new n x 1 variable (in this case, 24 x 1).
You'll note that my code simply puts your data into a cell array. It does not write anything to a file. You can put it in the loop were you were writing the data to a file but you can of course just omit the file writing part you did, and just keep the cell array storing part that I did.
Sorry if the original post or my reply was confusing. MATLAB is not responsible for the creation of session data or the text file. It is generated from 3rd party software to which I have limited configuration access. The first thing interpreted by MATLAB code is the aggregated text file, which may contain 3 sessions, 30 sessions, etc.
@Rob does my solution not work for you?

Sign in to comment.

Products

Release

R2018a

Asked:

on 24 Jun 2018

Commented:

on 24 Jun 2018

Community Treasure Hunt

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

Start Hunting!