Random code deletion during saving/running

6 visualizzazioni (ultimi 30 giorni)
Taylour Beigh
Taylour Beigh il 10 Gen 2021
Commentato: Taylour Beigh il 11 Gen 2021
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)

Walter Roberson
Walter Roberson il 11 Gen 2021
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
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?
Taylour Beigh
Taylour Beigh il 11 Gen 2021
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.

Community Treasure Hunt

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

Start Hunting!

Translated by