Spliting Cell Into Multiple Columns

57 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2018-9-6
@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 2018-9-6
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.

请先登录,再进行评论。

采纳的回答

Robert U
Robert U 2018-9-6
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 个评论
Quang Phung
Quang Phung 2018-9-6
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 2018-9-7
  • 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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Big Data Processing 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by