How to produce a consolidated source file? - matlab

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 :)

Related

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.

clearing many MATLAB functions in one single file

I have many small functions in matlab, is it possible to put them all in one file so my work will look clearer?
i tried writing a small script and then adding the functions but is didn't work
any idea on how do do it?
It is not possible to have several functions in one file, because the function is accessed via the file name. That is why a function has to have the same name as the file name.
If you only access the "small" functions inside one other function, then you can put the small functions in the file of the other function, but then they are only accessible to this one function. This is called local functions in MATLAB. For example you have a file test.m with:
function x=test(y,z)
x = add(y,z)
end
function a=add(b,c)
a = b + c;
end
You can then only use add inside test, but you can use test just as usual.
What I usually do is put functions in subfolders. This helps you keep your path clean without any limitations. This also allows you to capsule your software better. The only thing you have to do is add the folder to your path with
addpath('subfolder');
If you have a function file, you can add other functions in that file.
If you have a script, you cannot add functions to it.
Note that if you put a function in a file, you cannot access the functions directly from outside your 'main' function scope.
In general I would recommend the use of folders, or proper file names to organize your functions, not stacking many of them in one file.
Extra
If your functions are really small and trivial, you could write a script with the declaration of anonymous functions for easy reuse. However this is probably not what you want.

Run matlab script function after deploytool [duplicate]

Background
Say I compile the following simple function in MATLAB
function foo(path_to_m_file)
disp([' Running ' path_to_m_file])
run(path_to_m_file);
end
The function foo just takes a path to an .m file and tries to run it.
However, when I actually try to run foo after compiling it:
./run_foo.sh $path_to_run_time $path_to_m_file
where path_to_m_file is a simple .m file with a statement such as:
a = 2;
I get the following error:
Error using ==> run
MATLAB:run:FileNotFound
However, I know that foo gets the correct path. For example, if I try replacing the line with run by the following two lines in foo
fID = fopen(conf_file, 'rt');
first_line = textscan(fID, '%s', Inf, 'Delimiter', '\n');
foo reads the corresponding line of the .m file. So the .m file is there, and the MATLAB engine can "see" it. Indeed I can even run eval on strings read with textscan.
So my questions are:
Why do I get the error above? Why doesn't foo run the .m file?
Update: See #strictlyrude27's answer below for what seems to be an answer to this question.
If the above doesn't work. Is there a way to get a MATLAB-compiled function to run an .m file that may have changed after compiling the original function?
The motivation for my second question:
I would like to have the ability to "update" an .m file that is part of the project without having to re-compile the full project. Any ideas for this would be greatly appreciated.
From the MATLAB Compiler's documentaton:
Compiled Applications Do Not Process MATLAB Files at Runtime
The MATLAB Compiler was designed so that you can deploy locked down functionality. Deployable MATLAB files are suspended or frozen at the time MATLAB Compiler encrypts them—they do not change from that point onward. This does not mean that you cannot deploy a flexible application—it means that you must design your application with flexibility in mind. If you want the end user to be able to choose between two different methods, for example, they both must be compiled in.
The MCR only works on MATLAB code that was encrypted when the component was built. Any function or process that dynamically generates new MATLAB code will not work against the MCR.
Some MATLAB toolboxes, such as the Neural Network Toolbox™ product, generate MATLAB code dynamically. Because the MCR only executes encrypted MATLAB files, and the Neural Network Toolbox generates unencrypted MATLAB files, some functions in the Neural Network Toolbox cannot be deployed.
Similarly, functions that need to examine the contents of a MATLAB function file cannot be deployed. HELP, for example, is dynamic and not available in deployed mode. You can use LOADLIBRARY in deployed mode if you provide it with a MATLAB function prototype.
Instead of compiling the function that generates the MATLAB code and attempting to deploy it, perform the following tasks:
Run the code once in MATLAB to obtain your generated function.
Compile the MATLAB code with MATLAB Compiler, including the generated function.
Tip: Another alternative to using EVAL or FEVAL is using anonymous function handles.
If you require the ability to create MATLAB code for dynamic run time processing, your end users must have an installed copy of MATLAB.
You can read read an m file, line by line and execute each line with the eval() function. There are restrictions on the format of the m file (no line breaks for example, each line must contain a complete MATLAB statement) but it does work and can add to your run time environment inside the compiled application. I use this technique to allow users to define configuration and data files for a compiled application I have developed.
Clearly, if your end user provides a poorly formed m file to evaluate, you will end up with difficult to resolve bugs.

Running an .m file from a MATLAB-compiled function

Background
Say I compile the following simple function in MATLAB
function foo(path_to_m_file)
disp([' Running ' path_to_m_file])
run(path_to_m_file);
end
The function foo just takes a path to an .m file and tries to run it.
However, when I actually try to run foo after compiling it:
./run_foo.sh $path_to_run_time $path_to_m_file
where path_to_m_file is a simple .m file with a statement such as:
a = 2;
I get the following error:
Error using ==> run
MATLAB:run:FileNotFound
However, I know that foo gets the correct path. For example, if I try replacing the line with run by the following two lines in foo
fID = fopen(conf_file, 'rt');
first_line = textscan(fID, '%s', Inf, 'Delimiter', '\n');
foo reads the corresponding line of the .m file. So the .m file is there, and the MATLAB engine can "see" it. Indeed I can even run eval on strings read with textscan.
So my questions are:
Why do I get the error above? Why doesn't foo run the .m file?
Update: See #strictlyrude27's answer below for what seems to be an answer to this question.
If the above doesn't work. Is there a way to get a MATLAB-compiled function to run an .m file that may have changed after compiling the original function?
The motivation for my second question:
I would like to have the ability to "update" an .m file that is part of the project without having to re-compile the full project. Any ideas for this would be greatly appreciated.
From the MATLAB Compiler's documentaton:
Compiled Applications Do Not Process MATLAB Files at Runtime
The MATLAB Compiler was designed so that you can deploy locked down functionality. Deployable MATLAB files are suspended or frozen at the time MATLAB Compiler encrypts them—they do not change from that point onward. This does not mean that you cannot deploy a flexible application—it means that you must design your application with flexibility in mind. If you want the end user to be able to choose between two different methods, for example, they both must be compiled in.
The MCR only works on MATLAB code that was encrypted when the component was built. Any function or process that dynamically generates new MATLAB code will not work against the MCR.
Some MATLAB toolboxes, such as the Neural Network Toolbox™ product, generate MATLAB code dynamically. Because the MCR only executes encrypted MATLAB files, and the Neural Network Toolbox generates unencrypted MATLAB files, some functions in the Neural Network Toolbox cannot be deployed.
Similarly, functions that need to examine the contents of a MATLAB function file cannot be deployed. HELP, for example, is dynamic and not available in deployed mode. You can use LOADLIBRARY in deployed mode if you provide it with a MATLAB function prototype.
Instead of compiling the function that generates the MATLAB code and attempting to deploy it, perform the following tasks:
Run the code once in MATLAB to obtain your generated function.
Compile the MATLAB code with MATLAB Compiler, including the generated function.
Tip: Another alternative to using EVAL or FEVAL is using anonymous function handles.
If you require the ability to create MATLAB code for dynamic run time processing, your end users must have an installed copy of MATLAB.
You can read read an m file, line by line and execute each line with the eval() function. There are restrictions on the format of the m file (no line breaks for example, each line must contain a complete MATLAB statement) but it does work and can add to your run time environment inside the compiled application. I use this technique to allow users to define configuration and data files for a compiled application I have developed.
Clearly, if your end user provides a poorly formed m file to evaluate, you will end up with difficult to resolve bugs.

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

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.