如何在matlab中检查字符串中的模式? [英] How to check a pattern in a string in matlab?

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

问题描述

我想检查特定模式是否在字符串中以执行某些操作

I want to check if a specific pattern was in a string to do some action

[filename pathname]=uigetfile
fullpath=[pathname filename]

在我的程序中我只会浏览图片,所有图片以该模式命名

In my program I will only browse for Pictures , all pictures are named with that pattern

* _ cam1.jpg,* _cam1.jpg,* _cam2.jpg * _cam2.jpg,* _cam3.jpg

*_cam1.jpg , *_cam1.jpg , *_cam2.jpg *_cam2.jpg , *_cam3.jpg

我想做什么来检查图像是否以cam1结束然后做一些逻辑

What I want to do to check if the image ends with cam1 then do some logic

if (filename.contain(cam1)
 then imread('1.jpg')
elseif (filename.contain(cam2)
 then imread ('2.jpg)

我知道在matlab中没有名为'contains'的方法,但这是一个展示我想要的例子。

I know that there no method called 'contain' in matlab but this is an example to demonstrate what I want.

推荐答案

对于更复杂的搜索,您可以使用正则表达式,但在这种简单的情况下,字符串比较就足够了。

For more complex searches you can use regular expressions, but in this simple case a string comparison is enough.

% Let the user choose only files that end in .jpg
[filename pathname]=uigetfile('*.jpg');
% Use fullfile to join file parts! It is OS independent. 
fullpath=fullfile(pathname, filename);

if length(filename) > 8 && strcmp(filename(end-8:end),'_cam1.jpg')
    stuff = imread(fullpath);
    ...
elseif length(filename) > 8 && strcmp(filename(end-8:end),'_cam2.jpg')
    stuff = imread(fullpath);
    ...
end

这不是最迷人的代码,但是它应该完成工作。

It is not the most glamorous code, but it should get the job done.

这篇关于如何在matlab中检查字符串中的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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