I have 4 distinct large files I'd like to load into memory simultaneously. The following example is not exactly what I'm doing -- but it illustrates to problem:
matlabpool open local 4
spmd
if labindex==1
R = rand(9,9);
else
R = rand(4,4);
end
end
size(R)
when I copy and paste this into the command prompt, R pops up in my work space. but if i save this to a *.m file, size(R) doesn't evaluate -- it gives me an error that R doesn't exist. I've tried using gather and initializing R as a global to no avail. Any ideas?
Okay:
I was saving this script as test.m.
Then I was calling it from the command line as:
run test.m
But then I ran it as-is from command line: test
'run' must spawn a subprocess which it kills after running or something. this must have been killing the variables. just calling the script straight correctly shares the scope of the workspace with the script.
Related
I'm trying to run .exe code inside Matlab terminal (R2018a) but I'm encountering some problems.
My .exe code runs normally on Windows cmd with the following generic command (an expected output file is created)
code.exe < input
In Matlab I tried 4 approaches:
system('code.exe < input');
dos('code.exe < input');
system('code.exe < input', '-echo');
system('set path=%path:C:\Program Files\MATLAB\R2018a\bin\win64;=% & code.exe < input');
They are returning the line below and no output file is created.
ans = -1.0737e+09
Adding the "&" character at the end of the line of attempt 1, as sugested by ref and many others, Matlab opens the Windows cmd in the correct folder but does not execute the .exe code properly (no processes are running in task manager), no output file is created, e.g.:
system('code.exe < input &');
I also tried the suggestion of this reference .exe run in matlab does not create two output files like it does when run in command prompt but I was not successful. This reference suggestion is also returning and no output file is created: "ans = -1.0737e+09"
Edit:
According to this link, user Walter Roberson says "-1073741511 is hex C0000139 which appears to correspond to the Windows error code for "Entry point not found". That indicates that you either tried to execute something that was inherently not properly created (such as if you tried to directly execute the DLL that did not have a main program), or else that the program you executed tried to use a DLL that could not be found."
Any insights on how to resolve this? Thanks.
The answer from this link solved my problem.
Basically, You will have to create a batch file similar to the one below and change the Matlab path according to your version.
fid = fopen('myBatchFile.bat','w')
fprintf(fid,'%s\n','set path=%path:C:\Program Files\MATLAB\R2018a\bin\win64;=%');
fprintf(fid,'%s\n','code.exe < input')
fclose(fid)
system('myBatchFile.bat')
I execute a matlab script which call other Batch scripts and generate with that other matlab instances (as if i opened matlab multiple times) at the same time at the end stay a lot of opened matlab windows , how can i to kill all matlab.exe instances running on my system. after finishing the execution of my main script ?
You can use quit or exit to terminate SAME matlab instance. https://www.mathworks.com/help/matlab/ref/quit.html
So, put these codewords at the end of the scripts so matlab instances close after scripts finish.
You cannot use matlab to terminate other running processes on your system. But you can run an external script - say some batch command that kills all matlab.exe instances running on your system. You do that by doing system('scriptName') in matlab, where scriptName is whatever you named this external bat script that kills the process.
To kill the process isn't a matlab question. One-liner solution can be found here:
https://superuser.com/questions/564878/killing-all-instance-of-a-specific-program-from-the-command-line
Solution for that script together with MATLAB code can be even found as a matlab question, coincidentally with exactly the "kill all instances" question:
https://www.mathworks.com/matlabcentral/answers/472-can-i-run-a-bat-file-with-matlab
I have tried running a script in the MATLAB command line and it says
>> run(ex1)
Undefined function or variable 'ex1'.
>> run(exp1.m)
Undefined variable "exp1" or function "exp1.m".
You're using run wrong. You need to encapsulate the script name as a string:
>> run('ex1.m');
You'll need to make sure that your working directory is set to where the script is located, because the above way to call run assumes local referencing.
Please read the documentation on run in the future: http://www.mathworks.com/help/matlab/ref/run.html
However, you can just type in ex1 in the command prompt and it'll still work... as long as you're in the working directory of where the script is run, and ensuring that you don't have any variables in your workspace that have the same name as the script file:
>> ex1
Is there a way of running a MATLAB script from Notepad++?
Obviously I have MATLAB installed on my computer. I know you can set a path for Notepad++to run when you hit F5, but when I set this path to my MATLAB.exe file, it simply opens another instance of MATLAB.
This is not what I want, I want the actual script in Notepad++ to be executed in the already open and running instance of MATLAB.
I'm afraid I'm not on my home computer at the moment to test this out, so the following is just a suggestion for you to try.
If you take a look at the NppExec plugin for Notepad++, you'll see that with it you can specify a command to be run when you hit F6 (like an enhanced version of hitting F5 in the regular Notepad++). You can also give it variables such as the path to the current file, and the name of the current file.
MATLAB (on Windows at least - I assume you're on Windows) makes available an API over ActiveX/COM. If you search in the MATLAB documentation for details, it's under External Interfaces -> MATLAB COM Automation Server. By running (in MATLAB) the command enableservice('AutomationServer') you will set up your running instance of MATLAB to receive instructions over this API.
You should be able to write a small script (perhaps in VBScript or something similar) that will take as input arguments the path and filename of the current file in Notepad++, and will then connect to a running instance of MATLAB over the COM API and execute the file's contents.
Set this script to be executed in NppExec when you hit F6, and it should then run the current file in the open instance of MATLAB.
As I say, the above is just speculation as I can't test it out right now, but I think it should work. Good luck!
Use NppExec add-on and press F6, copy paste the following and save the script:
NPP_SAVE
set local MATPATH=C:\Program Files\MATLAB\R2015a\bin\matlab.exe
cd "$(CURRENT_DIRECTORY)"
"$(MATPATH)" -nodisplay -nosplash -nodesktop -r "try, run('$(FILE_NAME)'),
catch me, fprintf('%s / %s\n',me.identifier,me.message), end"
then run (press F6; enter). Matlab Console and Plot windows still open and stay open. Error messages will be displayed in opening Matlab command window. Adding
, exit"
to the last command will make it quit and close again. If you want to run an automated application with crontabs or the like, check Matlab external interface reference for automation.
matlab.exe -automation ...
Also works in cmd terminal, but you have to fill in the paths yourself.
This is a usable implementation upon Sam's idea. First, execute MATLAB in automation mode like this.
matlab.exe -automation
Next, compile and execute this following VB in NppExec plugin. (which is to use MATLAB automation API)
'open_matlab.vb
Imports System
Module open_matlab
' connect to a opened matlab session
Sub Main()
Dim h As Object
Dim res As String
Dim matcmd As String
h = GetObject(, "Matlab.Application")
Console.WriteLine("MATLAB & Notepad++")
Console.WriteLine(" ")
'mainLoop
while True
Console.Write(">> ")
matcmd = Console.ReadLine()
' How you exit this app
if matcmd.Equals("!!") then
Exit while
End if
res=h.Execute(matcmd)
Console.WriteLine(res)
End while
End Sub
End Module
Then you'll get a matlab-like terminal below your editor. You can then code above and execute below. type !! to exit the terminal.
What it looks like
Tips: don't use ctrl+c to interrupt the MATLAB command, because it will kill the whole process instead.
I try to run a simple task on different machines of a cluster.
My configuration has been validated (it's ok). When I run the code
on the 'local' configuration, it works. But when I use the cluster
configuration, I get the following error :
Error using parallel_function (line 598)
Undefined function 'lafunc' for input arguments of type 'double'.
Error Stack : (No remote error stack)
Error in petittest (line 6) --ยป (petittest is my program's name)
parfor it=1:200
I try to modify the code to use "dfeval" instead of the parfor loop,
but I got the same kind of result (unrecognize function lafunc).
How do I get the other workers in the cluster to recognize the function
lafunc that I manually defined ?
The code is the following:
%%%%%%%%%%%%%
laconfig='/home/matlab/fred/LACED_DC1.mat';
setmcruserdata('ParallelConfigurationFile',laconfig);
matlabpool open
parfor it=1:200
yo=lafunc(it);
disp(yo)
end
matlabpool close
%%%%%%%%%%%
where the lafunc function is
%%%%%%%%%%%%%%
function [y]=lafunc(x)
y=x*x;
end
%%%%%%%%%%%%%%%%%%%%%%
Thanks a lot, every piece of info is useful to me!!
Make sure that your MATLAB script on all worker nodes running in the same working directory, and that other directories with functions you need are included in the path. So you can specifically set the working directory and path in your script:
matlabpool open
cd workdir
addpath funcdir
...
After you run matlabpool open it will run all path related commands on all workers. See here.
You can also open matlabpool with 'FileDependencies' parameter so all the workers will know where to look for required files. See the documentation for MATLABPOOL.