MATLAB thinks my entire script is commented, no idea why.

After coming back from holiday MATLAB can't seem to be able to open my script. Every line appears to be commented out, even though there's no comment '%' indicators (single line or paragraph). Atatched is a screenshot of my problem. This only happens for this particular script, or any saved copies (such as saving script.m to scriptV2.m, etc...). Running the code does nothing.
I can't copy and paste any of the code except into a new script file within MATLAB, but it preserves this strange comment behaviour. So I can't copy it to a .txt file, then back. Nor can I copy it here to show you - when I try, it just copies a single % symbol.
I've tried saving my file as a .txt, then opening it, but when it saves it puts spaces between each letter, like "T H I S", meaning I can't use the code or paste it back into matlab.
I can write new, working code at the bottom, if I create enough new lines. But I can't edit, use or recover any of the existing code.
Can anyone help? I've also attached a deleted version of the code (as the main script contains sensitive infiormation/data) - if anyone can get it working or knows a fix, I'd be most grateful, or I'll have to re-type the entire document...

 Accepted Answer

Cris LaPierre
Cris LaPierre on 22 Sep 2022
Edited: Cris LaPierre on 22 Sep 2022
How was this file created? When I open the attached file in MATLAB, there are a lot more spaces between each letter.
When I open it in a text editor, I see those spaces are NUL characters.
If I remove the null characters, it is no longer treated as a comment. To do this, I highlighted the first character in line one, pressed Ctrl + H to open Find and Replaace, and made sure Replace was empty (delete anything there, including a space), and then clicked Replace All.
Final result

4 Comments

The file was working as normal two weeks ago, and today it just opened in this semi-corrumpted state. I genuinely have no idea what happened. There's also an extra line break in the script on every line - those wern't there before.
Really strange that, if you open it in MATLAB 2019a, you don't see the spaces, so dont see the NULL chcracters.
Removing those nulls fixes my issue. Just have to undo all those extra line breaks that break the code.
Thanks!
Here's a script you could run that will read the a specified file, remove the null characters, and write the updated text to a new file.
Your file had 711 null chars.
filename = 'Strange_Behaviour.m';
rm = char(0); % Char to remove
% find specified char and remove
txt = fileread(filename);
sprintf('Removing %d null chars.', sum(txt==rm))
newtxt = strrep(txt, rm, '');
% write the new file to the same directory
newfilename = 'fixedfile.m';
filepath = fileparts(which(filename));
filename = fullfile(filepath, newfilename);
fid = fopen(filename,'w');
fprintf(fid,'%s',newtxt);
fclose(fid);
open(filename)
Thank you @Adam Danz, this is also really helpful! :)
The sample code I uploaded is only small part of a much larger script (1500+ lines). There were thousands of the NUL chars, so this sped things up.
Now the NUL chars are fixed/removed, I'll need to delete every other line in the code - for some reason, there was an additonal line break added between every line of code, so that'll need undoing to improve readability and fix some broken pieces of code.
You could remove the additional line breaks programmatically, too. I don't have the file open now but I believe the line breaks can be identified using char(10) in my second line of code. You can identify which lines of code contain only a line break character and then remove those lines.

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!