MATLAB Answers

Error using brace indexing with function hypot

6 views (last 30 days)
Tessa Kol
Tessa Kol on 10 Sep 2020
Commented: Tessa Kol on 10 Sep 2020
I have a number of data files where every data file is stored in a cell array. Thus, :
datac.1 is stored in expData{1,1} at time = 1 second
datac.2 is stored in expData{2,1} at time = 2 seconds
datac.3 is stored in expData{3,1} at time = 3 seconds
... etc.
Each data file contains the (x,y,z) coordinates of each particle at a different time step.
I have also a number of data files that contain the (x,y,z) velocities of each particle at a different time step. These are also stored in a cell array. Thus,:
datav.1 is stored in velData{1,1} at time = 1 second
datav.2 is stored in velData{2,1} at time = 2 seconds
datav.3 is stored in velData{3,1} at time = 3 seconds
I succesfully managed to make a pcolor plot for one data file, as shown below.
% Width of the grid bin,m
w = 0.6;
% Length of the grid bin, m
l = 0.06;
% Height of the grid bin, m
h = 2.3;
% Bins in x-direction
nx = 1000;
% Bins in y-direction
ny = 1;
% Bins in z-direction
nz = 1000;
% Total number of bins
q = nx*ny*nz;
% Volume of a single bin
v = (w/nx)*(l/ny)*(h/nz);
xbin = (w/nx);
ybin = (l/ny);
zbin = (h/nz);
x = (1:nx)*xbin;
y = (1:ny)*ybin;
z = (1:nz)*zbin;
[X, Z] = meshgrid(x,z);
% x-coordinate of particles
xc = expData{1800,1}(:,1);
% z-coordinate of particles
zc = expData{1800,1}(:,3);
% Velocity in x-direction
vx = velData{1,1800}(:,1);
% Velocity in z-direction
vy = velData{1,1800}(:,3);
% Root sum of squares velocity
V = hypot(vx,vy);
Vtot = griddata(xc,zc,V,X,Z);
figure(2)
pcolor(X,Z,Vtot)
shading interp
I want to the the same procedure as above for all my data files. Thus, I need to loop over all the cells. I tried the following:
for m = 1:length(files)
xc{m} = expData{m,1}(:,1);
zc{m} = expData{m,1}(:,3);
vx{m} = velData{1,m}(:,1);
vz{m} = velData{1,m}(:,3);
v{m} = hypot(vx{1,m}(:),vz{1,m}(:));
V = griddata(xc,zc,v,X,Z);
figure(2)
pcolor(X,Z,V)
shading interp
end
However, I get an error that brace indexing is not supported by the function hypot.
1) How can I loop over all the files and get the pcolor for every file?
2) How can I display the colorbar of the velocity?

  2 Comments

Matt J
Matt J on 10 Sep 2020
Here's the error I get, when I run your code
Undefined variable "expData" or class "expData".
Error in test (line 32)
xc = expData{1800,1}(:,1);
Tessa Kol
Tessa Kol on 10 Sep 2020
All the data files are assigned to expData. I imported the data files into matlab with another piece of code, which I did not post here. Sorry if that was not clear from my post. In the meantime I think I got some progress into my code:
for m = 1:2745
xc{m} = expData{m,1}(:,1);
zc{m} = expData{m,1}(:,3);
vx{m} = velData{1,m}(:,1);
vz{m} = velData{1,m}(:,3);
v = cellfun(@hypot,vx(:),vz(:),'UniformOutput',false);
V = griddata(xc{m},zc{m},v{m,1}(:),X,Z);
% figure(2)
% pcolor(X,Z,V)
% shading interp
end
I managed to assign the hypot function to every cell, that went well. And now I am waiting until matlab calculates V = griddata...
I takes a really long time though.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 10 Sep 2020
Nowhere in your first section of code do you define a variable named expData. MATLAB doesn't get as far as your hypot call in that section of code.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!