matlab code turned into unreadable symbols - matlab

I hit something wrong in Matlab and my code was transformed into unreadable strings of symbols (I . I suspect that this is a simple question for cs people, but I'm just an academic-in-training "end" user of code --that is, I know little theory and forget it easily, unfortunately.
I hit ctr+z but nothing happened, closed it and opened it again, but the symbols are still there. And it doesn't run, the error I get is:
The input character is not valid in MATLAB statements or expressions.
This is the beginning of my code, in its unfortunate current state:
MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Sun Oct 05 06:57:45 2014
"‰\*’fTøÄ^L3:!I]ƒÁCƒÒP>朳÷>—º0ç²öEEHÉm�0fÈçRHñ)—\¢ßZï³æ3öïû£óû�㬽ֻÞËó>ïûîZ‡£ñ-IŽj⻺ø«âÀ§ªú}ÕeßrÏè¼Qƒ3råó$G]µ¾O<ÈÎÉÊÈÍÊLuTø¨Õ4»ÕÁãò²ÇºFå–¯fØ P«iv«•8\¶\¶\¶œ¶œ¶œ¶Ff�¿â¼XuåÚ­Š½•¥bï•«yÙ¹Cs®��ÕÊ»áßüJ»ËWÓìV+Ù‘a' |5Ínµ²#\¶\¶\¶œ¶œ¶œ¶»†Ére9+F4±šVyU:£ÂæËVÓl$T¸0±÷òe…ɬ�9C]W 5kPNîØœ¼Ê«9¹ƒ+{'3ÍVBš­„´Jœ¶:8mupÚêà´ÕÁi«ƒÓV‡r~¨("¾ZAD|µ‚ˆr.±•�f+¡’¶~È°õC†­2lý�aë‡[?äåŒØx)'«8mVÓ*¯–ƒÓn5Í‘3nÐy¡<›—ýòÃùt[ùj†Ü_®°WI«¼·|YüGâ9ËåpT´‚V³s._% 7–¯VÜèPHÍ•72{„+?g°Ê/§íê{Sm÷¦&"_YD†­Ü+÷¦Úí%¹µÄ_IUG¼NVSv”×Éñw­õСþo‰‡E³Ä§ÐÍfæ&|ÓÈÍ©Y¥•ÞÆÍ°ëÏK‹×¿ô5°GÞзÃ�SØ„”Ýö/`mågf›ºê�;ŸÝÃÄéocÇÜ,µú‡
Ÿíçeƒ;îíáfr9c¯ú^¨ãûðöb§;ÞÙ^À¤ØÅǦ°‡÷ç§,Ø<…�¾°müá nöû{ÅÝÛ•zØCþ‡Îo×éÙ#gÿ;ãæc:ô[b2yÍÛ­¼L~åz<ìÏ>æ†|^ÜÓÃ=Oélžg¦8¿ØÄú~�­müIÞ°Ÿuæù­÷²FKöþ‰Òžr3if>÷²æÕ&¿™þ“¿‡ºÙŒ›“žªu£Îž•60Ù$)p Áî�†76`ŠÉÆÉ�õ|¬‰?í°ÎÞJ†4ÑÙù~XÖHܸS‡ß�?ÙdŠä‚d?ä&ûØ´£µû—°¯™ú
ѯ�:¯TUç5/îo ôxÄÇd8ëž1¿ûMè]ÕùOûÙßó…À]Ûô�ø,3Øöñ‡[¤ÞkÂîÛMÄ;ÉÇ—
ÏòÅqñŠØV½­q)ð±Û¤³Lx§õ¢sä1ß½ÎÝjÀn¦ä§XëE‘ãï}ågÌüágâß6?ô3¹\œ„�%>vR˜}C®ñ;釜b?»(àÒâ—
And so on.
Thanks!

Your file isn't code at all, it's a Matlab data file with the wrong extension. You can see this by running
>> x = magic(10); %// creates a 10x10 matrix
>> save('junk.m', 'x'); %// note .m extension rather than .mat
>> edit junk.m
You will see something like this in your editor window -
MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Sun Oct 05 14:21:38 2014
å³B1#V A6å³B1#V å³B1#V // etc etc, lots more junk here
which is what a .mat file looks like when you change its extension to .m and open it as code.
So, sadly, I think you have overwritten your code file with some data. If you rename your file to have a .mat extension and then load it in MATLAB, you will be able to see what the data is.
If you have some kind of backup, you may be able to get your code back. Otherwise you're out of luck.
P.S. I used to be "just an academic-in-training end-user of code" as well. I spent some time learning a little bit of CS theory and programming languages (on and off over the course of a year) and it has paid back 100x in productivity gains. Not only will you be able to solve many more issues on your own, you'll also be able to do things that you haven't even considered possible to automate your work, leaving more time for the "fun" bit of research. I highly recommend the time investment!

Check the Extension of your file:
In MATLAB you can use the command pcode, which preparses your MATLAB code to a form that is unreadable by humans, but runs exactly the same (actually, very slightly faster) as the original MATLAB code. What happens is that for each .m file you pcode, you'll get a new file with a .p extension. The .p file runs the same as the .m file, but is unreadable.

Related

Matlab figures lagging

Last week, Matlab crashed on my computer and gave a message regarding the figure driver or engine being changed (unfortunately, I do not remember exactly what it said). Ever since then, all of my figures have been extremely laggy and have appeared to have a lower resolution.
Has anyone run into this or have any troubleshooting advice?
I was able to solve this by creating a startup.m file in my MATLAB directory (located in Documents in my case). This file is called every time MATLAB is initialized. In this file I typed: opengl('save','hardware'). After staring MATLAB once with this startup.m file, I was able to delete this line of code from that file (this command changes opengl permanently, unless it is manually changed back).
Alternatively, I could have typed opengl hardware into the startup.m file. In this case this line of code would need to be left in the startup file.

Use older MATLAB save formats

I'm running a model that has a bunch of DLLs which read some .mat files.
When I use an old version of MATLAB (I think 2011a) to generate the files I get files that work okay, but when I create them with 2017a the files seem not to work with the same script.
I've used 2017 to read in the working 2011 file and then saved it, and these files also don't work.
I've also tried the above with the '-vXX' settings at all available values according to the help, with no success.
Example:
clear; load('v2011file.mat'); save('v2017copy.mat', '-v6', 'var1', 'var2', 'var3');
One thing that I have noticed between the two is that when they're selected in the "Current folder" browser, the preview always shows the 2017 files with the variable names in alphabetical order, regardless of the order that I saved them in, while the older 2011 file seems to maintain the order that they were saved. I can only assume that this is something related to a change in the way that files are saved - it might not be the problem but it does hint toward a change (it does this whether or not I include '-vXX' to use older formats).
It's probably worth noting that the 2011 files are created on XP, while the 2017 files are made on Windows 7.
Essentially I'm looking for anyone who might know whether it's possible for me to change the way the file is put together by MATLAB, rather than having to change the DLLs to accept a newer file.
It looks like I can work around the save order issue and have something that works by doing:
save('new2017file.mat', 'var1');
save('new2017file.mat', 'var3'. '-append');
save('new2017file.mat', 'var2', '-append');
Meaning I can put them in a specific order - I have to have the default save set to -v7 in preferences>general>.mat files too.
I wouldn't say no to a more elegant answer if there's one available though!

use matlab to open a file with an outside program and execute 'save as'

Alright, here's what I'm dealing with (you can skip to TLDR if all you need to see is what I want to run):
I'm having an issue with file formatting for a nasty conglomeration of several ancient programs I've strung together. I have some data in .CSV format, and I need to put it into .SPC format. I've tried a set of proprietary MATLAB programs called 'GS tools' for fast and easy conversion, but fast and easy doesn't look like its gonna happen here since there are discrepancies in how .spc files are organized now and how they were organized back when my ancient programs were written.
If I could find the source code for the old programs I could probably alter the GS tools code to write my .spc files appropriately, but all I can find are broken links circa 2002 and earlier. Seeing as I don't know what my programs are looking for, I have no choice but to try resaving my data with other programs until one of them produces something workable.
I found my Cinderella program: if I open the data I have in a program called Spekwin and save the file with a .spc extension... viola! Everything else runs on those files. The problem is that I have hundreds of these files and I'd like to automate the conversion process.
I either need to extract the writing rubric Spekwin uses for .spc files (I believe that info is stored in a dll file within the program, but I'm not sure if that actually makes sense) and use it as a rule to write a file from my input data, or I need a piece of code that will open a file with Spekwin, tell Spekwin to save that file under the .spc extension, and terminate Spekwin.
TLDR: Need a command that tells the computer to open a file with a certain program, save that file under a different extension through that program (essentially open*.csv>save as>*.spc), then terminate the program.
OR--I need a way to tell MATLAB to write a file according to rules specified by a .dll, but I'm not sure I fully understand what that entails.
Of course I'm open to suggestions on other ways to handle this.

Can I force visdiff to display more than the first 2000 bytes?

I have two binary files that I'm trying to compare using Matlab's built-in function visdiff, but it only displays the first 2000 bytes as a default. Is there any way to force the comparison tool to display the entire contents of both files side by side?
Edit the file matlabroot\toolbox\shared\comparisons\private\bindiff.m, where matlabroot is your MATLAB installation directory. On line 149, you'll see it sets the variable MAXLEN to 2000. Change this to something bigger (even Inf seems to work).
You may need to type rehash toolboxcache after making this change, in order to get MATLAB to notice.
Please note:
As you're making a change to the MATLAB source, this is at your own risk (it seems fine to me though). Keep a backup of the file you've edited.
That truncation at 2000 bytes is there for a reason - comparing the whole of larger binary files does seem to take quite a while, so be patient. Maybe try gradually increasing MAXLEN, rather than going straight to Inf.
I only have R2011b available to me right now, so if you're on a newer version the file path and line number I mentioned above may have changed. It was very easy to trace through the code from visdiff to comparisons_private to bindiff though, so unless they've changed the deeper structure of the Comparisons Tool between 11b and now, it will probably be very similar.

Extract .mat data without matlab - tried scilab unsuccessfully

I've downloaded a data set that I am interested in. However, it is in .mat format and I do not have access to Matlab.
I've done some googling and it says I can open it in SciLab.
I tried a few things, but I haven't found any good tutorials on this.
I did
fd = matfile_open("file.mat")
matfile_listvar(fd)
and that prints out the filename without the extension. I tried
var1 = matfile_varreadnext(fd)
and that just gives me "var1 = "
I don't really know how the data is organized. The repository described the data it contains, but not how it is organized.
So, my question is, what am I doing wrong in extracting/viewing this data? I'm not committed to SciLab, if there is a better tool for this I am open to that.
One options is to use Octave, which can read .mat files and run most Matlab .m files. Octave is open source with binaries available for Linux, Mac, and Windows. Inside of Octave you can load the file using:
load file
See Octave's manual section 14.1.3 Simple File I/O for more details.
In Scilab:
loadmatfile('file.mat');
(Source)
I had this same interest a few years back. I used this question as a guide. It uses Python and SciPy. There are options for NumPy and hd5f as well. Another option is to write your own reader for the .mat format in whatever language you need. Here is the link to the mat file format definition.