简体   繁体   中英

MATLAB: ??? In an assignment A(I) = B, the number of elements in B and I must be the same

I'm doing a project modelling the flow of sauce around pasta, using a BEMLIB code for stokes flow called prtcl_3d_ss_visualize.m which is available from "http://dehesa.freeshell.org/BEMLIB/". Here is the code:

%---
file2 = fopen('prtcl_3d_ss_visualize.m')
Npnt   = fscanf(file2,'%f',[1,1])
Nvert  = fscanf(file2,'%f',[1,1])
Nface  = fscanf(file2,'%f',[1,1])
vert   = fscanf(file2,'%f',[3,Nvert]);
wall   = fscanf(file2,'%f',[1,1])
fclose(file2)
%---

for i=1:Nvert
save = vert(2,i);
vert(2,i) = vert(3,i);
vert(3,i) = save;
end

Ic=0; 
for i=1:Nface
for j=1:Npnt
  Ic=Ic+1;
  fac(j,i) = Ic;
end
end

patch('faces',Nfac’,'vertices',vert’,...
      'FaceColor','y',...
      'FaceLighting','phong',...
      'BackFaceLighting','lit')
%light('Position',[1 3 2]);
%light('Position',[-3 -1 3]);
%material dull
%material shiny
%axis vis3d off
axis([-1.5 1.5 -1.5 1.5 -0.5 2.5 ])
%view(45,34)
xlabel('x')
ylabel('z')
zlabel('y')

xw(1)=-1.5; zw(1)=-1.5; yw(1)=wall;
xw(2)= 1.5; zw(2)=-1.5; yw(2)=wall;
xw(3)= 1.5; zw(3)= 1.5; yw(3)=wall;
xw(4)=-1.5; zw(4)= 1.5; yw(4)=wall;
xw(5)=-1.5; zw(5)=-1.5; yw(5)=wall;
patch(xw,zw,yw,yw); 
hold off

I have encountered several errors when inputing the code in matlab, the first of which i solved changing fopen('prtcl_3d') to fopen('partcl_3d_ss_visualize.m') which is the name of he file. The second error I came up against was where I input

patch('faces',Nfac’,'vertices',vert’,...
      'FaceColor','y',...
      'FaceLighting','phong',...
      'BackFaceLighting','lit')

Nfac' was orighinally fac'but i got error "??? Undefined function or variable fac" so I changed it to an already defined variable Nfac.

The problems I am now faced with the last part of the code. When I enter the first line

xw(1)=-1.5; zw(1)=-1.5; yw(1)=wall;

I get the error message: "??? In an assignment A(I) = B, the number of elements in B and I must be the same."

I get this for the rest of the xw,zw,yw inputs too, what am I doing wrong?

I solved changing fopen('prtcl_3d') to fopen('partcl_3d_ss_visualize.m') which is the name of he file.

No, you didn't solve it, you broke it. In the original script, partcl_3d_ss_visualize.m (or the version I found on the internets, at least), we have these lines:

file2 = fopen('prtcl_3d.net')
Npnt   = fscanf(file2,'%f',[1,1])
Nvert  = fscanf(file2,'%f',[1,1])
Nface  = fscanf(file2,'%f',[1,1])
vert   = fscanf(file2,'%f',[3,Nvert]);
wall   = fscanf(file2,'%f',[1,1])
fclose(file2)

This opens some sort of data file prtcl_3d.net , and loads in some required values (this file is presumably included somewhere in the code you downloaded). What you are making it do is make the script load itself and try and find some non-existent floating point numbers at the start of the file.

Having done this, it is likely that the output of wall is empty. Therefore yw(1) (1 element) and wall (zero elements) have different sizes and when you try to assign one to the other you get an error.

Important Note: It won't error earlier because it was able to open the file and try to read something - it is your job to check if what was read in is correct. In this case, just manually looking at your workspace would have been enough. In other cases, error checking can be done using functions such as isempty , isnumeric , size , and so on depending on what sort of output you're expecting.


Regarding fac / Nfac , I imagine that this script does not stand alone and you are supposed to run another script or function before this one that creates the correct variables.

It says on that website:

BEMLIB is a boundary-element software library of Fortran 77 (compatible with Fortran 90) and Matlab codes accompanying the book by C. Pozrikidis, A Practical Guide to Boundary Element Methods with the software library BEMLIB,'' Champan & Hall/CRC, (2002). Chapters 8-12 of the book contain the BEMLIB User Guide

I suggest you need the BEMLIB User Guide to help you understand what the code does and how to use it, and until you do understand that you shouldn't be making any changes to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM