MATLAB error when storing values in a matrix

In summary, the speaker has set up a function file called BSform.m and is experiencing an error when trying to store a vector in a matrix using a for loop. The error occurs at specific values for i and the speaker has tried transposing the values and converting them to integers, but the problem persists. They are seeking help in identifying the cause of the error.
  • #1
Firepanda
430
0
Basically I have a function file set up called BSform.m, I'm confident this has nothing to do with my problem

My problem is this driver file for the function

X=zeros(101,2);
for i = 0:0.01:1
[C1,P1]=BSform(200,200,2,0,0.02,i);
Y=[C1,P1]
X(100*i+1,1)=Y(1,1)
X(100*i+1,2)=Y(1,2)
end

My output Y ALWAYS gives me a 1x2 vector (this is what I want, the function is working as intended)

I am trying to STORE this vector in my X matrix

Everything is FINE up until i get to i=0.14, and I've no idea why

The error I get is:

? Attempted to access X(15,1); index must be a positive integer or logical.

Error in ==> File2 at 5
X(100*i+1,1)=Y(1,1)It's really weird since if I manually input this Y value it manages to access it fine and store it in X. I thought about overriding it and moving on, but then it screwed up when it hit X(29,1) and again at X(30,1) then again a few steps more, really irregular pattern.

Any idea what's causing this? I've tried transposing it all, and the same problem hits at the same values for i.

Thanks
 
Physics news on Phys.org
  • #2
Why don't you convert index to integer, e.g.

X(int32(100*i+1),2)=Y(1,2)
 

What is a MATLAB error when storing values in a matrix?

A MATLAB error when storing values in a matrix is an error that occurs when there is an issue with the code used to assign values to a matrix. This could be due to a syntax error, incorrect variable assignments, or other issues with the code.

Why am I getting a MATLAB error when storing values in a matrix?

There are a few possible reasons for getting a MATLAB error when storing values in a matrix. Some common reasons include using incorrect syntax, trying to assign values to a matrix that is the wrong size, or trying to assign values to a matrix using variables that have not been properly defined.

How can I fix a MATLAB error when storing values in a matrix?

The best way to fix a MATLAB error when storing values in a matrix is to carefully check your code for any syntax errors or incorrect variable assignments. You may also need to check the size and dimensions of your matrix to ensure that it can properly store the values you are trying to assign to it.

Is there a way to prevent MATLAB errors when storing values in a matrix?

While it is impossible to completely prevent MATLAB errors when storing values in a matrix, there are a few things you can do to minimize the chances of encountering these errors. These include using proper syntax, carefully defining and assigning variables, and double-checking the size and dimensions of your matrix before attempting to assign values to it.

Can I get help with a MATLAB error when storing values in a matrix?

Yes, there are several resources available for getting help with MATLAB errors when storing values in a matrix. You can consult the MATLAB documentation, reach out to the MATLAB community for assistance, or seek help from a more experienced MATLAB user or instructor.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
801
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
985
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
851
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
Back
Top