Porting MATLAB code from Windows to Mac - macos

I have just switched from a PC to a Mac, and I am finding that lots of my MATLAB code previously written when I had a PC does not work on my Mac! I have been working on MATLAB for a while now, but I am not an expert yet.
After searching around for differences between PC and Mac, I noted that a few things indeed differed, but I'd love to hear about whether I need to go through all my yet written MATLAB code and update it manually to make it work on my Mac.
Please let me know what best to do here.
Example:
clear all
cd 'c:\users\sss\Desktop\MATLAB\project\DataFile\'
load data
cd ..
Why doesn't this work? Is it because of the backslash required for MATLAB on a Mac?

Of course, if you try to access a Windows-style path on a Mac, it will error.
MATLAB includes a set of functions that make it fairly easy to make your code cross-platform with respect to these sorts of issues. Take a look at, for example, the functions fullfile, fileparts, filesep, pathsep, ispc, and ismac.
I'm afraid that for the moment, you'll probably need to recode things to be either Mac-specific or to be cross-platform using the functions above.

One way is to have a path variables or variables set which determine where your data is held. You can even use computer or ismac and ispc to automatically switch to the correct version:
if ispc
dpath = 'c:\users\sss\Desktop\MATLAB\project\DataFile\';
elseif ismac
dpath = '/Users/sss/MATLAB/project/DataFile/';
end
load (fullfile(dpath, 'data.mat'));
If you have multiple files in subdirectories of /MATLAB/project/, you can set a project directory (similarly to matlabroot but pointing at where your files for that project are kept), and then use fullfile to select the correct subdirectory.
e.g. given a directory in proot that points to wherever /MATLAB/project/ is on the appropriate computer, these produce filenames which are in /MATLAB/project/data and MATLAB/project/output respectively:
datain = fullfile(proot, 'data','data.mat');
dataout = fullfile(proot,'output','output.mat');

Related

MATLAB exist('resources', 'dir') found directory. What is it, and where is it really located?

I have been writing a MATLAB script for handling a lot of files and variables for my thesis work. One thing I do is storing the work directories on a file. Working with this have lead me to a lot of trouble with lines equivalent to
exist('resources', 'dir') == 7
I am not that experienced with Windows directories not aimed toward end users, I thought this would surely fail since it does not look like a valid path on any drive. However, it does succeed finding a folder. I suspect there are more of these directories. Or is it a folder in MATLAB's search paths?
What are they? And where are they?
It should be a folder on Matlab search path, likely this one:
exist([matlabroot '/toolbox/simulink/simulink/resources'], 'dir') == 7
I guess what you want is a subfolder relative to pwd, then you can do
exist('./resources', 'dir') == 7 % work for all OS
to check if the folder exists.

Accessing files using Racket

I want to make a DrRacket program that can copy all of the files from a given directory (taking it off of a USB Camera (when it's plugged in it's seen as a mass storage device)) and paste them into a folder on my computer's hard drive. For whatever reason I'm unable to figure out DrRacket's implementation of a path on the computer (ie. for the Desktop on a Windows Machine it would be: C:\Users\Mike\Desktop) I read the help desk and still can't figure this out. Any suggestions as to where I should look to clear up my confusion? I think the function call that I'll need to implement this idea is:
(copy-directory/files src dst)
After I figure that out I'm going to work on a GUI for it so it operates at the click of a button.
You may be looking for the content about paths. You can create a path using build-path. A lot of the path-manipulating functions, though, can take strings as well. So you should be able to say something like:
#lang racket
(copy-directory/files "C:\\Users\\Mike\\Desktop\\..." ...)
with the ... replaced appropriately.

Getting safe temp folder in Windows

I need to get a safe temp folder where I could store temporary files for my application, but so far my research has lead me to conclusion that all approaches I've found are flawed.
The first idea was to use GetTempPath function, but that causes two problems:
The folder might not exist, so I would have to truncate folders one by one up to root, and recreate them if they do not exist back to full path (error prone, tedious)
From "Larry Osterman's WebLog" click it seems that GetTempPath might fallback to USERPROFILE or Windows directory and extract whole lot of files right in there, which is SUPER BAD(TM)!
In the same post, there is a suggestion to use GetEnvironmentVariable, but this seems a dangerous function to me (missing TMP & TEMP envvars for instance).
Is there a cleaner function I could use? Seems that SHGetKnownFolderPath has no clue what temp folder is.
Your program is probably not the only one to rely on GetTempPath, so it's reasonable to expect it to return a proper writable path. Especially since Windows automatically initializes the TMP and TEMP environment variables for you; someone would have to go to some trouble to override them, and it would be their responsibility to make sure the change did not mess up their system.
I would go ahead and assume GetTempPath works properly, and worry about failures when you try to create the temporary file - there are other errors that might occur at that time that you need to check for anyway.
An idea would be to get the path where your application is (GetModuleFileNameEx combined with GetModuleHandle(NULL) and GetCurrentProcess) since this directory cannot be deleted under windows as long as your application is running from it (maybe I'm wrong ...some years ago I couldn't do this :) ) and in this directory create a temporary directory.
Your first bullet point is the solution. Wrap it up in a method so that you don't duplicate code.
According to this answer, Boost's Filesystem library can be used for this.

Programmatically hiding many files when creating hybrid iso with hdiutil

I'm trying to script the creation of a hyrbid (iso/joliet/hfs) iso with hdiutil. I can, for example, build an iso that hides things on the mac side like so:
hdiutil makehybrid -o foo.iso -hfs -joliet -iso -hide-hfs "{foo/bar.txt,foo/other.rtf}" foo
That's just an example of course, but the point is I can get it to hide say seven or eight example files I specify like that, with spaces in the filenames and verious dots and underscores.
But for my actual real-deal script I need to list in the neighborhood of 70 files, which does not seem to work when I test it. The whole string is being passed in correctly, I know this because when you turn on '-verbose' it prints the string and says it doesn't match anything.
So my best guess is it has something to do with the length of the string passed in, but I don't see anything in the docs indicating that. Any ideas? Think it's a bug? An alternative way of accomplishing this?
This is on Mac OS X 10.5.8, btw.
Two [UPDATE, make it Three] (untested) suggestions:
use the -plistin option to
specify all the parameters;
(better) try organizing the files to be
hidden into directories, if
necessary, so you can easily hide
them by directory-specific globs
rather than having to spell out each
file.
[UPDATE] you could try using mkisofs from cdrtools to make the ISO image. MacPorts has a supported port of it. It could be that the code in hdiutil was originally based on an earlier version. In any case, you have the advantage of access to the source code and perhaps figuring out what the limitations are.
P.S. There seems to be a couple of minor nits with the MacPorts port. In particular, the
man pages are installed in the wrong directory. [UPDATE: fixed in 3.00_1]

Visual Studios: Getting standard font... files?

I've run into another problem while fixing up my game to use this library.
SDL.NET, the library I'm using for graphics and input in my VB.NET app, has its own special Font class which is entirely separate from System.Drawing.Font. Here are its two constructors:
public Font(string fileName, int pointSize)
public Font(byte[] array, int pointSize)
Both need a file, in glaring contrast to System.Drawing.Font (which just needs the font family name). I'm not sure where these files are. My first instinct was to look in Windows\Fonts for the ones I want to use, but... you can guess how that approach failed.
I need to find the files for Cambria, DotumChe, and Photo (all of which came installed on the computer). My program is very far from completion, so I'm not worrying about the legal complications of what I'm trying to do. I just want to find the files and get them in my project so I can move on. Is there a place on my computer where I can find them?
A C# example is available at Steve's Tech Talk. It uses P/Invoke to get the fonts folder path.
I'm assuming you plan on resolving legal/licensing issues before distributing the game.

Resources