如何在matlab脚本中定义路径? [英] How to define a path in matlab script?

查看:504
本文介绍了如何在matlab脚本中定义路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个matlab脚本可以说image_process.m和函数image_process接受一个参数是图片。
我想为图像文件夹定义一个路径,现在我可以调用image_process路径image_name。
我知道我可以做image_process path / image_name,但是我需要为我的函数添加额外的参数。

解决方案

这很容易,只需使用 fullfile 函数来合并

  function foo(imageName,cDir) 
fullFileName = fullfile(cDir,imagesName)
%用fullFileName做一些事情b
$ b end

如果您使用的是脚本而不是函数,只需将其转换为函数即可。

无论如何,脚本是不好的练习,因为它们混淆了全局工作空间,并造成严重破坏。
任何脚本都可以转换为函数。

$ p> foo('peppers.png','C:\MySpecialDir')


I have a matlab script lets say image_process.m and the function image_process accepts one argument which is the picture. I want to define a path for the images folder so now I can call image_process path image_name. I know I can do image_process path/image_name but I need to add the extra argument to my function.

解决方案

It is quite easy, just use the fullfile function to combine

function foo(imageName,cDir)
    fullFileName = fullfile(cDir,imagesName)
    % Do something with fullFileName

end

If you are using scripts instead of functions, just convert it to functions.
Scripts are bad practice anyway, because they mess with the global workspace, and cause havoc.
Any script can be converted to function.

Call your function like this:

foo('peppers.png','C:\MySpecialDir')

这篇关于如何在matlab脚本中定义路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆