m file code sometimes loss a "end" and corruput the file on hard disc.

2 views (last 30 days)
I encountered some very rare error (~1 occurance/year).
The error is that when I run matlab project/functions, very rarely the code is corrupted, one time it is a variable name that was changed at somewhere which cause an error(The old variable name is updated, and run for many times OK, then suddenly error take place one day saying that a variable is not defined), the other time is the lossing of a "end" statement in middle, so the code jump to the FUNCTION END and cause error of no argout.
This is strange since I have not edit this code for many months but still using it everyday. But it just happened just now.
I thought it might be caused by too heavy usage of my xps13 laptop for past 7 years. I used Matlab too heavily to automate my enumourous works and my laptop seems to be at the final stage of its life.
Not sure if someone else also encountered this kind of problem.

Accepted Answer

Jan
Jan on 23 Mar 2022
Edited: Jan on 23 Mar 2022
It is extremely unlikely, that the 3 characters "end" vanish inside a text file, while the rest of the computer runs without a crash. Such behaviour would be a sign of pure magic.
This effect happens frequently and you find many similar questions. In all (!) cases, there have been deterministiv reasons, which the user was not aware of. Of course, this does not allow to prove, that this is true in your case alos, but it is a very strong evidence.
Compare the changed files with your backups. Check, when these files have been changed or if you have several files with the same name and are running another version, because the current directory is the folder, which contains the other M file.
If the 3 characters "end" would be changed to some strange uni-code characters, a failing SSD or hard disk would be more plausible. But a clean vanishing is too unlikely.
Use a chkdsk (assuming you work with Windows) and a tool like CrystalDiskInfo to check the integrity of the disk. But have in mind, that there is another important source of problems, which you did not consider yet. A version history, which contains all changes of each file, is required for a reliable programming.
  1 Comment
raym
raym on 23 Mar 2022
Ah Yes, I think I got the reason for the missing end:
When I update code months ago, I inserted a if-end statement but missed a end in the root level, so the FUNCTION END was used instead.
Then all the running later meet the condition, thus I cannot find this defect.
Today I use another condition thus it does not meed, then jump to end with no argout.
The first scenario should also be the similar cause: The code was not fully tested but can work in certain condition for many time, until the condition was not met.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!