Random code deletion during saving/running

Hi all! I'm new to this program and to this forum- I'm learning this program as I take a course for it through my university.
As I clicked "run" to check my work- MATLAB decided to throw away all of my code I had written, and replace it with this beauty:
MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Sun Jan 10 15:14:21 2021,
I'll post the actual error as well.
Here is the code that I had wrote prior to this error.
-------------------------------------------------------------------------------------------------------------------------------------------
% Part 1
prompt = 'Enter an integer n'
n = input(prompt)
mustBePositive(n)
prompt = 'Enter a second integer m'
m = input(prompt)
mustBePositive(m)
if n < m
disp('M VALUE TOO BIG, OR N TOO SMALL')
elseif n == m
disp('NICE!')
elseif n > m
disp('WE CAN MAKE THIS WORK!')
end
M = magic(n);
M(:,m) = []
% Part 2
A = randi([50,100],3,6);
save('randfile.dat','A')
B = randi([50,100],2,6)
save('NAME_HW1.m','B')
-----------------------------------------------------------------------------------------------------------------------------------------------------
Note It HAS worked prior and has worked even after this error when I copied my code and repasted it into another file and retried to run. I just would like to find out why this happens seemingly randomly, and hopefully I could solve this issue prior to submission.

1 Commento

Stephen23
Stephen23 il 11 Gen 2021
Modificato: Stephen23 il 11 Gen 2021
"MATLAB decided to throw away all of my code I had written, and replace it with this beauty"
The basic issue is that you have confused binary .mat files (which are for saving data in) with .m files (which are text files of MATLAB code). If you mix them up, then of course at worst you can expect to lose data and/or code, or at best not have things work as expected.
save('NAME_HW1.m','B') % <- wrong file extension!
I strongly recommend that you use the correct file extensions for their intended purposes, it will make your life much easier.

Accedi per commentare.

Risposte (1)

save('NAME_HW1.m','B')
That is a request to save the variable B into the file NAME_HW1.m . As you did not ask for -ASCII and .m is not a file extension understood to be asking for ASCII, it saved in MAT binary file format.
Note that if you had used -ASCII then the entire .m file would have been written over with the content of the variable, just in text form.

8 Commenti

How would I approach fixing this? Thank you, I appreciate the answer but unfortunately I’m unsure where to start to fix it aside from knowing my last line is the fault.
You have not been clear as to what the goal of the line is ?
The line
save('randfile.dat','A')
already demonstrates saving a variable to a binary file. Normally you would
save('randfile.mat','A')
by convention, but .dat could be used as well.
Apologies on the lack of clarity, we want to:
1) save the variable ‘A’ to a file called “randfile.dat”
2) append variable ‘B’ to the original file (NAME_HW1.m)
You can use save() with the -ASCII and -double and -append flags
However, do you really just want the .m file to suddenly go into a bunch of numbers? It would not be an executable .m file any more if you did not. Perhaps it would be better to write it in the form of an assignment statement?
If you want to write it in the form of an assignment statement, I suggest you read about fopen() 'a' permission, and about fprintf(). I might also suggest mat2str() .
"append variable ‘B’ to the original file (NAME_HW1.m)"
Why are you trying to save data in an .m file? M-files are for code, not data.
@Walter The assigment simply asks for me to append that matrix B to the .m file. The .m file that it referrers to is the same file that we're looking at. I understand that it wont be executibe, but it doesnt make sesnse as to why my instructor would want it that way.
@Stephen The assignement I'm given strictly asks for that
Attached is the guidlines given. Perhaps im interpreting them wrong?
I really appreciate the responses thus far.
Stephen23
Stephen23 il 11 Gen 2021
Modificato: Stephen23 il 11 Gen 2021
The assignment is not very precisely defined, but my interpretation is that it is asking for the 2x6 matrix to be appended to (most likely text) randfile.dat (which already contains a 3x6 matrix), thus would give a file containing one single 5x6 matrix. Several clues point to this interpretation:
  • the compatible number of columns of the matrices,
  • the term "append",
  • the last line uses the singular, implying only one matrix.
Although there could be other interpretations, they would all add complexity to this assignment, which is unlikely to be the goal. Most likely the intent of the assignment is to work with basic numeric matrices and their basic text representation in a basic text file and its basic import into one basic matrix. Why make this more complex?
I was able to get into contact with my professor, and after discussion he told me that indeed, I am suppose to append the 2x6 onto the tail of the 3x6 making it a 5x6.
Thank you for the help.

Accedi per commentare.

Richiesto:

il 10 Gen 2021

Commentato:

il 11 Gen 2021

Community Treasure Hunt

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

Start Hunting!

Translated by