Is it possible for a MATLAB script to behave differently depending on the OS it is being executed on? - matlab

I run MATLAB on both Linux and Windows XP. My files are synced among all of the computers I use, but because of the differences in directory structure between Linux and Windows I have to have separate import and export lines for the different operating systems. At the moment I just comment out the line for the wrong OS, but I am wondering if it is possible to write something like:
if OS == Windows
datafile = csvread('C:\Documents and Settings\Me\MyPath\inputfile.csv');
else
datafile = csvread('/home/Me/MyPath/inputfile.csv');
end
This is also a more general question that applies in cases where one wants to execute system commands from within MATLAB using system('command').

You can use ispc/isunix/ismac functions to determine the platform or even use the computer function for more information about the machine
if ispc
datafile = csvread('C:\Documents and Settings\Me\MyPath\inputfile.csv');
else
datafile = csvread('/home/Me/MyPath/inputfile.csv');
end

To follow up on Amro's answer, I was going to just make a comment but struggled with the formatting for the code.
I'd prefer to split the OS selection from the file read.
if ispc
strFile = 'C:\Documents and Settings\Me\MyPath\inputfile.csv';
else
strFile = '/home/Me/MyPath/inputfile.csv';
end
try
datafile = csvread(strFile);
catch
% setup any error handling
error(['Error reading file : ',strFile]);
end
That way if I need to change the way the file is read, perhaps with another function, it's only one line to change. Also it keeps the error handling simple and local, one error statement can handle either format.

Just to add a minor point to the existing good answers, I tend to use fileparts and fullfile when building paths that need to work on both UNIX and Windows variants, as those know how to deal with slashes correctly.

In addition to using the various techniques here for dealing with path and file separator differences, you should consider simply trying to avoid coding in absolute paths into your scripts. If you must use them, try to put them in as few files as possible. This will make your porting effort simplest.
Some ideas:
Set a fileRoot variable at some early entry point or config file. Use fullfile or any of the other techniques for constructing a full path.
Always use relative paths, relative to where the user is operating. This can make it easy for the user to put the input/output wherever is desired.
Parameterize the input and output paths at your function entries (e.g., via a system specific context object).

If the directory structures are within your home directory you could try building a single path that can be used on both platforms as follows (my Matlab is a bit rough so some of the syntax may not be 100%):
See here for how to get the home directory for the user
Create the path as follows (filesep is a function that returns the file separator for the platform you are running on)
filepath = [userdir filesep 'MyPath' filesep 'inputfile.csv']
Read the file
datafile = csvread(filepath)
Otherwise go with Amros answer. It is simpler.

Related

Running a Matlab p-file coded for Windows on a MacOS

I have several Windows sourced p-files internally coded with the '\' file separator that I want to run on Matlab under macOS.
I get errors caused by the '\' because macOS uses '/'.
eg The pfile tries to call a file named "model\xyz' which causes a warning:
"Name is nonexistent or not a directory: model\ "
1) Is there code that I might insert somewhere to recognise the 'model\' call from the pcode file and change it to 'model/' before it is used by MATLAB addpath?
2) Is there a generic fix I could apply to the addpath code?
3) Or better still is there a way to modify the Windows p-file without access to its source code so that it will run under macOS?
There are several things you can do (none of which are particularly easy; listed here in increasing order of how nasty I think they are):
Contact the author of the code and ask them to fix it.
Install an older MATLAB version (R2007b <= ver < R2015b, I think) which allowed debugging (stepping into) p-files within MATLAB, then, assuming that there is some line in the original source code that does
filepath = ['model' '\' 'xyz.m'];
step until you see filepath appear in the workspace (having the wrong path in it), then simply edit the value to the correct path, and hope for the best.
(Essentially the same idea as before, but on newer MATLAB versions, VERY DIFFICULT to pull off) Obtain an external debugger, attach to the MATLAB process, run the p-file and scan the memory for the contents of filepath. When you discover it, change the value to the correct path and detach/disable the debugger.
If this code relies on some external (and open source) function to get (or use) the path, you can modify its behavior to output the string you want. This requires some knowledge about the source code.
Find/make a tool for decoding p-files and fix the resulting source code yourself.
For completeness I describe how my problem was solved:
As suggested by Dev-iL I was eventually able to locate the author and he modified his code. This is the best solution but it took some time and is not always possible.
Based on: https://au.mathworks.com/matlabcentral/answers/117110-dealing-with-and-windows-vs-unix-for-path-definition I located a module (I've forgotten its name) within the Matlab package which handles file calls and intercepted all incoming file path names containing the '\' Windows separator, and replaced them with the always acceptable '/'. This quick and dirty fix worked until solution 1. was obtained.
Thanks to all who responded so quickly to this question.

How to produce a consolidated source file?

Does MATLAB have the following capability: take source code that directly includes other .m files and output the source that would result from merging all included files?
For example, consider script_one.m:
% some matlab code
script_two
% more matlab code
I would like to programmatically generate the .m file that would result from copying and pasting the contents of script_two.m into script_one.m. This is difficult to do with normal scripting tools because I would essentially need a MATLAB symbol table to determine which identifiers correspond to sourceable scripts. I highly doubt that Matlab provides such a facility, but am open to other ideas.
The "use case" is the need to modify the source (using sed) but the changes need to propagated to any dependent scripts, such as script_two.m. As I don't have a listing of the dependent scripts, they can only be identified by going through the source manually (and it needs to be done on a large number of dynamically created files).
Some details on the use case:
The main script (script_one) is called with dynamically created header files, e.g., matlab [args] -r 'some definitions; script_two; script_three; others; main_script();quit()'. This is run on machine A; for load balancing, it may need to be run instead on machines B, C, etc, which mount the file system of A at some point. Any paths in the included .m files (which are mainly used as headers) would need to be essentially chrooted to work on the new host. The simplest solution would be to preprocess the code which was generated for machine A, using sed to replace all paths for the new host (B, C, etc.). It can of course be solved by making the changes in matlab, but a sed one-liner is a more attractive solution in terms of parsimony.
In general, no, it's not possible in MATLAB. What you want is a language feature common to languages that require compilation step before execution, but this is not MATLAB's language model, and therefore, it is only doable via hacky wacky language abuse.
You could, conceivably, create a master script, which takes care of coordinating the generation of new source files, and executing them via eval():
[o,e] = system('<your sed command here, to generate script_one.m>');
% ... some more code
% execute newly generated M-file
[outputs] = eval('script_one');
But I hope you see and agree that this turns into spaghetti really quickly.
Executing a script with changing contexts and parameters is exactly what the function language feature was invented for :)

Script for running (testing) another matlab script?

I need to create a matlab mfile that will run another matlab file with default values given in a txt file. It's ment to be useful for testing programs, so that user may specify values in a txt files and instead of inputing values every time he starts the program, my script will give the program default values and user will only see the result.
My idea is to load tested file into a variable, change 'variable=input('...');' for variable = default_variable;, save it to tmp file, execute, and than delete tmp file. Is this going to do the job?
I have only two problems:
1) How to eliminate the problem of duplicated variable names - i mean this must work for all scripts, i don't know the names of variables used in tested script.
2) As I wrote before - is this going to work fine? Or maybe I missed a easier way to do it - for example maybe I don't have to create a tmp file?
I really need your help!
Thanks in advance!
If the person who has to edit the default values has access to Matlab, I would recommend saveing the values in a mat file and loading them when needed. Otherwise you could just write a smalls cript that contains the assignment to certain variables, but make sure to keep this small. For example:
maxRuns = 100;
clusters = 12;
So much for setting up the defaults. Regarding the process my main advice is to wrap the thing that you want to test into a function. This way variables used in the code to call the 'script' will not interfere as a function gets its own separate workspace. Check doc function if you are not familiar with them.

MATLAB - Stitch Together Multiple Files

I am new to MATLAB programming and some of the syntax escapes me. So I need a little help. Plus I need some complex looping ideas.
Here's the breakdown of what I have:
12 seperate .dat files, each titled something like output_1_x.dat, output_2_x.dat, etc.
each file is actually one piece of a whole that was seperated and processed
each .dat file is approx. 3.9 GB
Here's what I need to do:
create a single file containing all the data from each seperate file, i.e. I need to recreate the original file.
call this complete output file something like output_final.dat
it has to be done in MATLAB, there are no other alternatives (actually there maybe; see note below)
What is implied:
I will have to fread each 3.9 GBfile into chunks or packets, probably 100 mb at a time (using an imbedded loop?)
these packets will have to be read then written sequentially
after one file is read then written into output_final.dat, the next file is automatically read & written (the master loop).
Well, that's pretty much it. I did a search for 'merging mulitple files' and found this. That isn't exactly what I need to do...I don't need to take part of a file, or data from files, and write it to a new one. I'm simply...concatenating...? This would be simple in Java or Perl, but I only have MATLAB as a tool.
Note: I am however running KDE in OpenSUSE on a pretty powerful box. Maybe someone who is also an expert in terminal knows a command/script to do this from the kernel?
So on this site we usually would point you to whathaveyoutried.com but this question is well phrased.
I wont write the code but i will give you how I would do it. So first I am a bit confused about why you need to fread the file. Are you just appending one file onto the end of another?
You can actually use unix commands to achieve what you want:
files = dir('*.dat');
for i = 1:length(files)
string = sprintf('cat %s >> output_final.dat.temp', files(i).name);
unix(string);
end
That code should loop through all the files and pipe all of the content into output_final.dat.temp (then just rename it, we didn't want it to be included in anything);
But if you really want to use fread because you want to parse the lines in some manner then you can use the same process:
files = dir('*.dat');
fidF = fopen('output_final.dat', 'w');
for i = 1:length(files)
fid = fopen(files(i).name);
while(~feof(fid))
string = fgetl(fid) %You may choose to parse the string in some manner here
fprintf(fidF, '%s', string)
end
end
Just remember, if you are not parsing the lines this will take much much longer.
Hope this helps.
I suggest using a matlab.io.matfileclass objects on two of the files:
matObj1 = matfile('datafile1.mat')
matObj2 = matfile('datafile2.mat')
This does not load any data into memory. Then you can use the objects' methods to sequentialy save a variable from one file to another.
matObj1.varName = matObj2.varName
You can get all the variables in one file with fieldnames(mathObj1) and loop through to copy contents from one file to another. You can then clear some space by removing the copied fields. Or you can use a bit more risky procedure by directly moving the data:
matObj1.varName = rmfield(matObj2,'varName')
Just a disclaimer: haven't tried it, use at own risk.

Matlab and addpath

I have recently been working with Matlab. My question stems from my usage over a few months and is something that I can't seem to solve. I have an external SVM toolbox (OSU-SVM) that I would like to interface to with my project. I am able to get the entire system to work when I add the path of the toolbox manually (Right click -> Add to Path -> Selected Folders and Subfolders). What I would like to do is add the folder in a script. I've tried the "addpath" command but for some reason I can't get it to find the library relative to the m-file (script) that I run the command from. The following is an example of the code:
% Add OSU SVM system
addpath(genpath('./osu-svm/'));
The reason the I'd like to add the path using a relative folder to the M-file is that the code needs to run in a different environment that will not have the toolbox install. The code is also going to be executed in a different OS to the one I am developing on. That is, I am running a Windows Matlab to develop the code and need to run the finished system on a Linux machine. The process of running my files needs to be as painless as possible and shouldn't require much input from the user. That is why I'm specifically trying to avoid manual addition of the path.
On a side note a similar problem occurs when I wish to use "uigetfile" using a relative path. I believe there is something I am missing that will help me solve both of these simultaneously. Any help would be greatly appreciated.
Instead of './osu-svm/' alone use
fullfile('.','osu-svm')
The reason it does not work for you on windows is that you are using forward slash file separators. Full file will make a file string containing the correct file separator for each OS.
The genpath example in the matlab documentation also uses fullfile
http://www.mathworks.co.uk/help/techdoc/ref/genpath.html
Furthermore, the '.' is kinda unnecessary as it just means "relative to the current directory" and can be left out of the command. Perhaps you meant one directory up?
'..'
???