Convert a non uniform cell array of cell arrays to matrix

5 ビュー (過去 30 日間)
Tessa Kol
Tessa Kol 2020 年 9 月 20 日
回答済み: Sindar 2020 年 9 月 25 日
I have the following code:
%% Loading the data
rhoPart = 2540;
files = dir(fullfile(uigetdir,'\**\*.data*'));
[~,Index] = natsort({files.name});
files = files(Index);
folders = {files.folder};
folder_groups = findgroups(folders);
k = 1;
for i = 1:length(files)
fid = fopen(fullfile(files(i).folder,files(i).name),'r');
%% Reading the data
% Read all the data from the file
dataRead = textscan(fid,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f','HeaderLines',1);
frewind(fid);
% Write headerline N, time, xmin, ymin, zmin, xmax, ymax, zmax
runData{k} = strsplit(fgetl(fid), ' ');
% Write only the x, y, and z components of the particles, particle radius,
% z component+ particle radius and volume of the particle
expData{k} = [dataRead{1}(:,1) dataRead{2}(:,1) dataRead{3}(:,1) dataRead{7}(:,1) dataRead{3}(:,1)+dataRead{7}(:,1) rhoPart*(4/3)*pi*(dataRead{7}(:,1).^3)];
% Write only the vx,vy,vz of the particles and magnitude
velData{k} = [dataRead{4}(:,1) dataRead{5}(:,1) dataRead{6}(:,1) sqrt(dataRead{4}(:,1).^2 + dataRead{5}(:,1).^2 + dataRead{6}(:,1).^2)];
fclose(fid);
k = k + 1;
end
%% Classify (into a structure)
runData_struc = splitapply(@(x) {x}, runData, folder_groups);
expData_struc = splitapply(@(x) {x}, expData, folder_groups);
velData_struc = splitapply(@(x) {x}, velData, folder_groups);
This will give me the following cell array of runData_struct (see pictures)
As you can see this is a cell array in a cell array. I want to convert the 1x9 cell into a 1x9 double. For example:
runData_struc{1,1}
row 1: 1x9 double
row 2 :1x9 double
row 3: 1x9 double
...
row 2741: 1x9 double
runData_struc{1,2}
row 1: 1x9 double
row 2 :1x9 double
row 3: 1x9 double
...
row 2745: 1x9 double
I tried the following, but it gave me NaN as result:
  9 件のコメント
Stephen23
Stephen23 2020 年 9 月 24 日
編集済み: Stephen23 2020 年 9 月 24 日
If you reverse the order then you do not need to call frewind, giving simpler and more efficient code.
Simply read the header first and then the rest of the file data (the 1 specifies how many lines to read:
% Read headerline N, time, xmin, ymin, zmin, xmax, ymax, zmax
Headerline = textscan(fid,'%f%f%f%f%f%f%f%f%f',1);
% Read all the data from the file
dataRead = textscan(fid,'%f%f%f%f%f%f%f%f%f%f%f%f%f%f');
Tessa Kol
Tessa Kol 2020 年 9 月 25 日
Thank you both for the suggestions and I will adapt my code according to it. Can I accept an answer to close this post?

サインインしてコメントする。

採用された回答

Sindar
Sindar 2020 年 9 月 25 日
(see comments on question for details not related to the title question)
A1 = cell2mat(runData_struc{1});
A2 = cell2mat(runData_struc{2});
if that doesn't work, something like this might (but I'm not the cellfun expert)
A1 = cell2mat(cellfun(@cell2mat,runData_struc{1},'UniformOutput',false));
A2 = cell2mat(cellfun(@cell2mat,runData_struc{2},'UniformOutput',false));

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by