从Matlab执行批处理文件

时间:2016-05-13 04:02:51

标签: r windows matlab batch-file cmd

我有一个Matlab函数,可以找到此函数在我的电脑中的路径,然后在同一目录上运行一个bat文件。这个bat文件用于执行R脚本,但由于一个奇怪的原因,它无法执行此操作。

这是我的Matlab功能:

function [] = myFunction(arg)

  % Find the directory of the executing script
  thisDir = fileparts(mfilename('fullpath'));

  % Save arg as a csv on this directory, this will be read by my R script
  tmpDir = strcat(thisDir,'/tmp.csv');
  csvwrite(tmpDir,arg);

  % Specify the command to run
  dosCommand = ['call "' thisDir '/runRscript.bat"'];
  dos(dosCommand);

end

bat文件包含以下代码:

"C:\Program Files\R\R-3.2.2\bin\x64\R.exe" CMD BATCH runRScipt.R

当我在Matlab中运行该函数时,我收到以下消息:

  

C:\ Users \ ... mypath ...>" C:\ Program Files \ R \ R-3.2.2 \ bin \ x64 \ R.exe" CMD批次   runRscript.R

由于我在Matlab中收到此消息,我毫不怀疑它正在查找和读取批处理文件,但它无法执行R脚本。我知道bat文件按预期工作,因为我可以通过命令行运行它(命令应该是" dosCommand"在Matlab脚本上)或者在.bat文件上单击两次。

2 个答案:

答案 0 :(得分:0)

请尝试使用Rscript.exe而不是R.exe。 R.exe在交互式mdoe中运行R代码,而Rscript以批处理模式运行代码。理想情况下,您应该在与R可执行文件相同的路径中找到Rscript可执行文件(即" C:\ Program Files \ R \ R-3.2.2 \ bin \ x64"在您的情况下)

答案 1 :(得分:0)

我找到了答案。由于一个奇怪的原因,dos()命令不起作用,但system()命令将完成这项工作。然后代码将如下所示:

function linkDomainClassesWithUseCases(dictionary,regExp,usecases)
    Session.Output usecases.Count & " use cases found"
    dim usecase as EA.Element

    'loop de use cases
    for each usecase in usecases
        Repository.WriteOutput "Link to Logical Data Model", "Processing use case: " & usecase.Name, 0
        'first remove all automatic traces
        removeAllAutomaticTraces usecase
        'get all dependencies left
        dim dependencies
        set dependencies = getDependencies(usecase)
        dim scenario as EA.Scenario
        'loop scenarios
        for each scenario in usecase.Scenarios
            dim scenarioStep as EA.ScenarioStep
            for each scenarioStep in scenario.Steps
                'first remove any additional terms in the uses field
                scenarioStep.Uses = removeAddionalUses(dependencies, scenarioStep.Uses)
                dim matches
                set matches = regExp.Execute(scenarioStep.Name)
                dim classesToMatch 
                set classesToMatch = getClassesToMatchDictionary(matches, dictionary)
                dim classToMatch as EA.Element
                for each classToMatch in classesToMatch.Items
                    Session.Output "scenarioStep.Uses before " & scenarioStep.Uses
                    if not instr(scenarioStep.Uses,"LDM-" & classToMatch.Name) > 0 then
                        if len(scenarioStep.Uses) > 0 then 
                            'add a space if needed
                            scenarioStep.Uses = scenarioStep.Uses & " "
                        end if
                        'add the name of the class
                        scenarioStep.Uses = scenarioStep.Uses & "LDM-" & classToMatch.Name
                    end if
                    'create the dependency between the use case and the Logical Data Model class
                    linkElementsWithAutomaticTrace usecase, classToMatch
                    Session.Output "adding link between " & usecase.Name & " and Logical Data Model class " & classToMatch.Name & " because of step " & scenario.Name & "." & scenarioStep.Name
                next
                'save scenario step
                scenarioStep.Update
                scenario.Update
            next
        next
    next
end function

function linkElementsWithAutomaticTrace(sourceElement, TargetElement)
    dim trace as EA.Connector
    set trace = sourceElement.Connectors.AddNew("","trace")
    trace.Alias = "automatic"
    trace.SupplierID = TargetElement.ElementID
    trace.Update
end function

批处理文件:

  

“C:\ Program Files \ R \ R-3.2.2 \ bin \ x64 \ R.exe”CMD BATCH runRScipt.R