can someone please help me understand this error?

why this give me this
Matrix index is out of range for deletion.
Error in del_emp (line 14)
Age(i)=[];
function [] = del_emp (e)
emp=R_Data();
i = search_emp (e);
if i>0
Name = emp.Name;
ID = emp.ID;
Age = emp.Age;
Employee_Position = emp.Employee_Position;
Years_of_Exeperience = emp.Years_of_Exeperience;
Status = emp.Status;
Name(i) = [];
ID(i)=[];
Age(i)=[];
Employee_Position(i)=[];
Years_of_Exeperience(i)=[];
Status(i)=[];
n = length (ID);
name1 = string (Name);
po1 = string (Employee_Position);
status1 = string(Status);
fid = fopen('C:\Users\ibrah\Desktop\me.txt', 'w');
for j=1:n
fprintf(fid,'%s \t %d \t %d \t %s \t %d \t %s \n',name1(j),ID(j), Age(j), po1(j), Years_of_Exeperience(j), status1(j));
end
fclose (fid);
else
disp('oOps!!..Invalid entry as the employee is not Existed');
end
end

댓글 수: 1

The error message “Matrix index is out of range for deletion” typically occurs when you’re trying to delete an element at an index that doesn’t exist in the matrix. In your case, it seems like the error is occurring at the line
Age(i)=[];.
This could be happening because the index i returned by the search_emp(e) function is greater than the length of the Age array. It’s also possible that search_emp(e) is returning 0 or a negative number, which would also be an invalid index.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2024년 4월 23일

댓글:

2024년 4월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by