MATLAB error when storing values in a matrix

  • Context: MATLAB 
  • Thread starter Thread starter Firepanda
  • Start date Start date
  • Tags Tags
    Error Matlab Matrix
Click For Summary
SUMMARY

The forum discussion addresses a MATLAB error encountered when attempting to store values in a matrix within a driver file for the function BSform.m. The user experiences an indexing issue when the loop variable i reaches certain values, specifically at i=0.14, leading to the error message indicating that the index must be a positive integer. The solution proposed involves converting the index to an integer using the int32 function, which resolves the indexing error and allows for proper storage of the 1x2 vector in the matrix X.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Familiarity with matrix operations in MATLAB
  • Knowledge of function file creation and usage in MATLAB
  • Basic understanding of data types and type conversion in MATLAB
NEXT STEPS
  • Learn about MATLAB data types and type conversion functions
  • Explore MATLAB matrix indexing and common errors
  • Investigate debugging techniques in MATLAB for function files
  • Study best practices for writing and organizing MATLAB scripts and functions
USEFUL FOR

MATLAB programmers, data analysts, and anyone troubleshooting matrix operations and indexing issues in MATLAB scripts.

Firepanda
Messages
425
Reaction score
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
Why don't you convert index to integer, e.g.

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

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
2
Views
2K