Spliting Cell Into Multiple Columns

58 visualizzazioni (ultimi 30 giorni)
Quang Phung
Quang Phung il 5 Set 2018
Commentato: Robert U il 7 Set 2018
I have been having issues separating my cell data into multiple columns. But I seem to be encountering problems with trying to split my 1 column cell data into 4 columns.
I have attached my code below:
%%Data Import:
% Open File:
FileID = fopen('TCOMP244.TXT');
% Read File
TCOMP_CELL = textscan(FileID,'%s','Delimiter','\n');
% Close File:
fclose(FileID);
% Data Extraction:
DATA_CELL=TCOMP_CELL{1,1};
%%Data Extraction:
% Iteration Initialization:
[k, j, l, m, o, p, r] = deal(1, 1, 1, 1, 1, 1, 1);
% Cell Initialization:
Primary_Chain = cell((ceil((length(DATA_CELL)-182)./89).*20),1);
Primary_Am_Chain = cell((ceil((length(DATA_CELL)-211)./89).*15),1);
Secondary_Am_Chain = cell((ceil((length(DATA_CELL)-235)./89).*15),1);
%%Primary Fission Chain:
for i=182:89:length(DATA_CELL)
for k=0:19
Primary_Chain{j,1} = DATA_CELL{i+k};
j = j+1;
end
end
%%Principal Americium Chain:
for l=211:89:length(DATA_CELL)
for n=0:14
Primary_Am_Chain{m,1} = DATA_CELL{l+n};
m = m+1;
end
end
%%Secondary Americium Chain:
for o=235:89:length(DATA_CELL)
for q=0:14
Secondary_Am_Chain{p,1} = DATA_CELL{o+q};
p = p+1;
end
end
This leads to following output:
What should I do to separate the cell into multiple columns.
*My intent is to take the latter 3 columns and convert them into a floating point matrix for use in a separate calculation.
Thanks in advance.
  6 Commenti
Stephen23
Stephen23 il 6 Set 2018
@Quang Phung: exactly which data do you need to import from that file? Can you please show us which values you need.
Quang Phung
Quang Phung il 6 Set 2018
Hi,
As I explained previously, the program I use to generate this data calculates various properties of nuclides at discrete points in time (determined by the user). In this case, it calculates the data from 3 different chains. Image 1 details such chain (The primary chain), which always starts at line 182. The other two chains start at lines, 211 and 235 (which can be seen in my nested loops). Each chain then restarts at a new time 89 lines down (regardless of the number of time steps the user selects).
The chain data is calculated at each time step (in this case 13 times, but the number is dependent on the number of time steps the user imports).
The current code I have reads the data from TCOMP244.txt using textscan and (presumably converts each line into a string).
The three nested loop then reads the data from each chain and coalesces each individual chain into a composite chain. E.g. Primary_Chain contains all the data from the primary chain (20 nuclides by 13 intervals = 260).
My intent is to split the chain data from a 260x1 cell into a 260x4 cell. Where each column is the various nuclide data and the nuclide name. I intend to delete the first column (nuclide names) and convert the remaining 3 columns from a cell into a matrix. My issue is splitting the cell into 4 columns.
The data I need can be seen in Image 1 and Image 2 (that I posted in response to Jonas). Image 3 is the cell data I extracted from the primary chain (Image 1 and Image 2). You can see that the data repeats itself starting at line 21 of the cell.

Accedi per commentare.

Risposta accettata

Robert U
Robert U il 6 Set 2018
Hello Quang Phung,
for your data "Primary_Chain" you could use the following code fragment in order to
  • seperate the columns inside cell
  • delete 1st column
  • convert to array of double
% separate columns
Test = cellfun(@(cIn) strsplit(cIn,' ')',Primary_Chain,'UniformOutput',false);
Test = [Test{:}]';
% delete first column
Test(:,1) = [];
% convert to double
TestOut = cellfun(@str2num,Test);
Kind regards,
Robert
  2 Commenti
Quang Phung
Quang Phung il 6 Set 2018
Hi Robert,
Thank you so much, the cell splitting function works perfectly. Could you explain how you created your code fragment. I'd like to be able to understand how this works for future reference.
Thanks!!!
Robert U
Robert U il 7 Set 2018
  • strplit() with delimiter ' '(space) is applied to each cell of cell array Primary_Chain
  • output is a cell array again that contains the strings split to cells
  • recover sub-cells via calling all sub-cells and collect in an array (to not loose structure mind the transpose()-command twice, once within cellfun() and next after data has been collected
  • delete first column straight forward
  • apply str2num() to each cell of Test, output will be numeric array of doubles

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Big Data Processing in Help Center e File Exchange

Prodotti


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by