Populate cell array dynamically
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Rob Miller
am 24 Jun. 2018
Kommentiert: Rob Miller
am 24 Jun. 2018
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!
0 Kommentare
Akzeptierte Antwort
Paolo
am 24 Jun. 2018
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 Kommentare
Paolo
am 24 Jun. 2018
What bug? The output of
output{:}
has 24 elements, which contains the repetitions you specified in your first post.
Weitere Antworten (1)
Image Analyst
am 24 Jun. 2018
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 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!