Search code examples
bashmatlablsf

Run matlab command line and automate running unix server bjobs


I'm running a chunk of matlab code called fit.m this way,

clear all;
load('/pathtomatfile/alldata.mat')
count = rxw1;
count = double(count);
x_cov = ltgbd;

alldata.mat has several data of interest, i.e.,

rxw1
rbg2
rfd1
wop3, 
ltgbd,
etc.

I can run such script from the command line for a given data of interest (in my example count=rxw1),

matlab -nodisplay -nojvm -nosplash -r ${pathtoscript}fit -logfile lfile.txt

However, I need to automate the running so I can tell matlab to make count = any of the other datasets in the mat file. I.e., I'd like to the script in parallel for different datasets but I'd need something like:

matlab -nodisplay -nojvm -nosplash -r ${pathtoscript}fit -logfile lfile.txt DATAOFINTERESTHERE (i.e., rxw1 count will equal rxw1, etc.)

Is it possible to do what I am suggesting? How would I automate the script to run for any of the datasets I choose by giving the name of the dataset as an input parameter when I call the script?

Once I have that I plan to actually run them all in parallel by submitting jobs through LSF but giving the name of the data of interest as an input, something like this in mind:

bsub -q week -R'rusage[mem=8000]' \
"matlab -nodisplay -nojvm -nosplash -r ${pathtoscript}fit -logfile lfile.txt DATAOFINTEREST"

Sorry if it's too basic of a Q but I am not familiar with running matlab command line.


Solution

  • You can make fit a function, instead of script. The fit function can have a single input variable pointing to the right data file. Then you can run

    matlab -nodisplay -nojvm -nosplash -r "cd ${pathtoscript}; fit('${dataofinterest}');exit;"
    

    EDIT: added this detailed fit fun.

    Your fit function should look something like

    function fit( variableName )
    %
    % run fit for a specific variable from the mat file
    % variableName is a string, e.g. variableName = 'rgb1';
    %
    
    ld = load('/pathtomatfile/alldata.mat');
    count = ld.(variableName); % access variables in mat file as struct fields
    count = double( count );
    % and I believe you can take it from here...
    

    EDIT: Similar solution loading mat file into a struct to handle the loaded variables can be found here with some more details.