'How can I run inputs from R through a matlab function?

I have a matlab code that I would like to run 3 inputs through that I have in R (delta_x, cutoff_1, cutoff_2).

First I would like to know how to save these in order to run them through the matlab code. delta_x is an r dataframe with around 1000 lines of data in a single column. Cutoff 1 and 2 are both just integers

The code is much longer than that shown below so it would take far too long to convert into R and I am on a tight schedule and not very familiar with matlab.

Essentially, what I would like to do is run my 3 inputs that I have in r through the matlab code that I have been provided with. Is anyone aware of how I can do this? I have read something about matlabr package?? but not been able to figure it out.

Matlab code looks like the following for reference:

    function [start_end]=plume_finder(delta_x,cutoff_1,cutoff_2)
    
    [pks,pksloc]=findpeaks(delta_x);% find the peaks
    valley=find(islocalmin(delta_x)==1);% find the valleys
    list_va=delta_x(valley);%find the values at the valley
    %There is two possibliltes, the valley presents first or the peak present
    %first. So we have to use 'shift' to make adjustment for this.
    if valley(1)>pksloc(1)
        shift=0;
    else
    shift=1;
    end
upperlim=min(length(pks),length(valley));% Find where to stop
%pksloc(:,2:end), indicating the whether this peak point could meet the
%requirements(2:whether the peak value greater than the cut-off, 3:whether
%the peak value has significant difference from valley point on its left,
%4:whether the peak value has significant difference from valley point on
%its right--> 1 stands for yes, and 0 stands for no)
for i=1:upperlim
    if pks(i)<cutoff_1
        pksloc(i,2)=0;
    else
        pksloc(i,2)=1;
    end
end
for i=2:upperlim
    if (pks(i)-list_va(i-1+shift))<=cutoff_2
        pksloc(i,3)=0;
    else
        pksloc(i,3)=1;
    end 
end


Solution 1:[1]

  1. In MATLAB, write a matlab script that can load ascii .csv files, process data and save results to new ascii .csv files.

  2. In R, save the variables to ascii .csv files.

  3. In R, invoke matlab script in batch mode through system() and wait for the generation of new processed files.

  4. In R, load the processed data file, and do whatever is waiting for.

P.S.1. csv files are pretty straightforward but lose precision, HDF5 or SQLite formats can be used instead.

P.S.2. The whole thing can be turned around that some R script is invoked within matlab through file system and system calls. Or call both R and MATLAB from bash or python or something else.

P.S.3 It's also possible to pass data through a local network socket connection, I will leave the details for now.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1