how to compile an .m file which contains calling of another .exe file inside it - matlab-deployment

I have an .m file from which I call another .exe file on my machine with "system" statement. Now, I am going to use "deploytool" to create a console application. But, running this application couldn't run the .exe file inside it. Please tell me if there is any way to resolve this problem.
More detail:
These are the files which are used by main m.file: A.txt , B.xlsx, C.exe (I have put all of them in a folder). And this is part of m.file which utilizes these file:
1- m = xlsread('B.xls');
…
12- [status,cmdout] = system('C');
After running C.exe it should output a text file called “Out.txt” and then
…
13- fileID = fopen(‘Out.txt’);
14- links = textscan(fileID, ‘%s’, 7, ‘delimiter’, ‘,’);
…
But in the resulted standalone file when it reaches to line 12, can’t run C.exe! –
Thanks in advance!

Related

Why can't MATLAB find a script that is on MATLABPATH

I've just created a script file in MATLAB, but can not run it. The name of my script is getEnvFiles.m. When I first tried to run it, I got the following result:
>> getEnvFiles
'getEnvFiles' is not found in the current folder or on the MATLAB path, but exists in:
\\wsl$\ubuntu\home\me
Change the MATLAB current folder or add its folder to the MATLAB path.
So, I added this directory (which is actually the current directory) to the search path, but still got the same result:
>> addpath('\\wsl$\ubuntu\home\me')
>> getEnvFiles
'getEnvFiles' is not found in the current folder or on the MATLAB path, but exists in:
\\wsl$\ubuntu\home\me
Change the MATLAB current folder or add its folder to the MATLAB path.
When I check the path, it looks like this directory is on the path:
>> path
MATLABPATH
\\wsl$\ubuntu\home\me
I can further verify that this directory is my present directory:
>> pwd
ans =
'\\wsl$\ubuntu\home\me'
and that getEnvFiles.m is in this directory:
>> ls
. .emacs.d HarborData
.. .emacs~ RawHarborData
.bash_history .landscape at
.bash_logout .motd_shown getEnvFiles.m
.bashrc .profile test.m
.bashrc~ .sudo_as_admin_successful
.emacs
Is the issue that I'm using wsl (Windows Subsystem for Linux), or do I have some other misunderstanding?
Type rehash and then try running your script again. Even though you have added the new directory to your path, you need to update the path cache so that it knows about the new scripts it can see.
The problem seems to lie in WSL's ability to add new files to the directory. When I create a new script within MATLAB, and try to run it, I get the problem discussed above. However, running already existing files is not a problem. For now, my only solution is to close MATLAB after creating a new script and reopen it. Then, I can run it. Although, oddly, I can't open it in MATLAB's editor.....
This may or may not help, but ...
Sometimes you have to prepend execution with .\ in order to run scripts in PowerShell or from within the command prompt (in Windows, I'm not sure about other operating systems).
I get this error quite often, especially when not in Admin mode.
Does this work?
% Get path of executing script.
filePath = matlab.desktop.editor.getActiveFilename; % Note that this isn't necessarily the same as the output of 'pwd()'.
% Or: filePath = mfilename('fullpath')
% Make sure all nested directories of filePath are on the path, then tell MATLAB where you're working.
addpath( genpath( filePath ) ); % If this fails, try one folder up the tree:
% addpath( genpath( fullfile( filePath, '.' ) );
cd( filePath );
Alternatively, it looks like you might have written and saved the script, and then typed it into the command window to execute. If it's not a special type of script (Function/Class/GUI/etc.) then you can simply click 'Run' in the Editor tab (or the F5 key) and MATLAB should prompt you with a 'Change Folder' option, to which you should acquiesce.
If you're running this script through the WSL terminal, try my suggested code.

How to add user created .mat files to the search path after compilation

How can I allow user to add additional dependenies after the source code was compiled with mcc.
I was thinking about an empty folder next to the executable, where the users can add the needed .mat-files, but I can't add the folder path to my executable (since addpath is not allowed in deployed applications).
Any ideas?
This answer assumes that your code can be customised by data contained in one or more .mat files at runtime.
You can point your code to look at a folder where the optional .mat file(s) would be located.
For example in the users home folder with a sub folder being the name of your application (or in the local app data) or whereever...
If you want it in a sub folder where the exe is, you can do this as well, you find the exe path using (on windows):
[status, result] = system('path');
installpath = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
fprintf ( 'The exe install path is "%s"\n', installpath );
Then your code looks to load for example:
file2load = fullfile ( installpath, 'subFolder', 'runtimeCustomisation.mat' )
if exist ( file2load, 'file' ) == 2
"doSomething with the file"
end
Or something like that.
Recall that this is for dependencies which are .mat files only.

Load file in matlab ralative to script location

I have a simple script in matlab and I want to load a file. It seems it only works if the file is in the same dir as the script. If I add the file to a directory it does not read it.
For example:
fileID = fopen('myfile','r' ,'n', 'US-ASCII');
but when I put myfile in files:
fileID = fopen('files/myfile','r' ,'n', 'US-ASCII');
or
fileID = fopen('./files/myfile','r' ,'n', 'US-ASCII');
I get a -1 as a fileID. File cannot be read.
As per the comments, this is happening because you most likely added the path of where the script was located to your MATLAB path but you did not add the subdirectory where the file was in to your path. This is why it can't find the file. Therefore to avoid this in the future, you need to physically change the directory (i.e. the Working Directory) of where MATLAB is currently operating to where your script is stored.
It is then where local referencing should work. You can do this by either using the cd function, going to the top of your MATLAB window where you see the directory listing, clicking on the arrow to the right and pulling a drop down menu to change the directory, typing the actual directory you want by clicking on any blank space in the directory listing to enable a text box:
... or if you are running the code in the MATLAB editor, it'll request that you change directories as the script you are trying to run is not currently located in the working directory.
You can also programmatically add the subfolders in your script directory using mfilename, fileparts, genpath and addpath:
[dir, ~, ~] = fileparts(mfilename('fullpath')); % locate your script directory
addpath(genpath(fullfile(dir))); % add the folder and all subfolders to Matlab search directory
% then load your file.
fileID = fopen('myfile','r' ,'n', 'US-ASCII')
If it is also important that all outputs should be placed within the same directory as your script file, you can cd to your script directory:
cd(dir)

Run script file from other script file in Octave

I have several Octave script files that run tests, named test_1, test_2, etc. I want to have a script file that will run all the tests, without having to switch all the test_n files to function files. I've tried several variations on this:
#!/path/to/octave -q
addpath('/path/to/directory/containing/all/scripts/');
source(test_1.m);
source(test_2.m);
but I always get "error: invalid call to script /path/to/directory/containing/all/scripts/test_1.m".
(I've tried source_file(), run(), and just having the filename alone on the line.)
Is there any way to run script files from a script file in Octave?
Try
source test_1.m
or
source('test_1.m')
instead.
Your syntax implies test_1 is a struct variable and you're trying to access a field called m
Same with the run command (in fact, run simply calls source under the hood).
You can also call the script directly, if it's on the path. You just have to make sure you don't include the .m extension, i.e.
test_1
test_2
Just put the name of the included script, without .m extension on a separate line.
Lets have for example script 1: 'enclosed.m'
and script 2: 'included.m'. Then enclosed.m should look like:
% begin enclosed.m
included; % sources included.m
% end encluded.m

Create a batch file to copy and rename file

I need to write a batch file that copies a file to a new folder and renames it.
At the moment, my batch file consists of only this command:
COPY ABC.PDF \\Documents
As you can see, it only copies the file ABC.pdf to the network folder Documents.
However I need to change this so it renames the file ABCxxx.pdf, where xxx is a text variable that I would like to set somewhere in the batch file.
For example, if xxx = _Draft, then file would be renamed ABC_Draft.pdf after it is copied.
Make a bat file with the following in it:
copy /y C:\temp\log1k.txt C:\temp\log1k_copied.txt
However, I think there are issues if there are spaces in your directory names. Notice this was copied to the same directory, but that doesn't matter. If you want to see how it runs, make another bat file that calls the first and outputs to a log:
C:\temp\test.bat > C:\temp\test.log
(assuming the first bat file was called test.bat and was located in that directory)
type C:\temp\test.bat>C:\temp\test.log