MATLAB Answers

Find value in another cell array based on condition

4 views (last 30 days)
Tessa Kol
Tessa Kol on 11 Sep 2020
Answered: Tessa Kol on 14 Sep 2020
I have a folder containing the following files:
Every file has the same format, but for a different time step:
First (headerline): N, time, xmin, ymin, zmin, xmax, ymax, zmax
N Lines: x, y, z, vx, vy, vz, rad, q1, q2, q3, omex, omey, omez, xi
where N is the number of particles.
(x,y,z) are the coordinates
(vx, vy, vz) are the velocities
rad is radius of particle
time is time step
I have split the format into 3 seperate cell arrays as follow:
expData with format x, y, z, rad, z +rad, vol
vol is the volume of the particle, which I calculated from radius
velData with format vx, vy, vz, mag
runData with N, time, xmin, ymin, zmin, xmax, ymax, zmax
%% Loading the data
rhoPart = 2500;
files = dir(fullfile(uigetdir,'*.data*'));
[~,Index] = natsort({files.name});
files = files(Index);
expData = cell(length(files),1);
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
With the following code I calculated the total mass of particles for every time step:
for i = 1:length(files)
%Count the number of particles in the silo (silo outlet is located at z =
%0.3 m)
particlesInSilo{i} = find(expData{i,1}(:,3)>0.3);
% Multiply the number of particles with the volume of the particle
siloMass{i} = sum(expData{i,1}(cell2mat(particlesInSilo(i)),6));
end
Thus,
the total mass of siloV1.data.1787 is 3.6317 kg at time step 1.900577757 s
the total mass of siloV1.data.1820 is 3.4481 kg at time step 1.935675163 s
....
the total mass of siloV1.data.2714 is 0 kg at time step 2.886495821 s
With the following code I found when the total mass is 0 kg:
for i = 1:length(files)
siloMass_zero = find(siloMass{1,i}(:)<=0);
end
I want to find the corresponding time step when the total mass is 0 kg. How can I do that?
Note that the codes I presented in this post are only sections of my code.

  4 Comments

Show 1 older comment
Tessa Kol
Tessa Kol on 14 Sep 2020
I created a num arrays as you said, i.e. siloMass_d and time_d (see pictures below). Every column is 1 simulation. One simulation contains 5 data files. Each data file contains the same format of data but at a different time step.
Thus, simulation 1:
At t = 6.1446 seconds the total mass is 0.1674 kg
At t = 6.1496 seconds the total mass is 0.1674 kg
Simulation 2
At t = 4.6397 seconds the total mass is 0.3042 kg
At t = 4.6447 seconds the total mass is 0.3042 kg
..etc.
In total I have 81 simulations
With the code below I managed to in each simulation where the total mass is equal or below 0.17 kg.
for i = 1:81 %Number of simulations
siloMass_zero{i} = find(siloMass_d(:,i)<=0.17);
end
Now I want to find the corresponding time. How can I do that?
Tessa Kol
Tessa Kol on 14 Sep 2020
I found the solution already:
for i = 1:N_run
siloMass_zero{i} = find(siloMass_d(:,i)<=0);
time_dischargerange{i} = time_d(siloMass_zero{1,i});
time_dischrage{i} = time_dischargerange{1,i}(1,1);
end
But thank you for your input!
Mario Malic
Mario Malic on 14 Sep 2020
Great work!
You can post an answer to your question and accept it yourself so other people can find it useful if they encounter problem similar to yours.

Sign in to comment.

Accepted Answer

Tessa Kol
Tessa Kol on 14 Sep 2020
Here the code that was succesfull for me:
for i = 1:N_run %Number of simulation runs, which is for me 81 simulations
siloMass_zero{i} = find(siloMass_d(:,i)<=0); % Find all the values in the numeric array that are smaller than or equal to zero
time_dischargerange{i} = time_d(siloMass_zero{1,i}); % Find the corresponding time by indexing
time_dischrage{i} = time_dischargerange{1,i}(1,1); % Extract only the first time step value, if multiple time steps are found
end

  0 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!