필터 지우기
필터 지우기

How do I find array elements that meet some condition with strcmp?

조회 수: 4 (최근 30일)
Chelsea
Chelsea 2014년 11월 6일
댓글: Chelsea 2014년 11월 6일
Hi,
I have a vector of image files. I would like to pull out only a subset of these files which meet a certain condition. The file names are numbers, and are organized (i.e., files 1100-1199 are one group, 1200-1299 another, etc.). I tried to use the find function, but since these are file names and not simply numbers, I don't think that will work. Should I use "strcmp"?. I'm not familiar with it. My code I have so far is below... Thanks! I'm working with Psychtoolbox.
close all;
clear all;
clc % clear command window
% Images
Images = {'1100.bmp';'1202.bmp';'1203.bmp';...}
% Shuffle images
Images=Shuffle(Images)
% Display matched image
MatchedImages=find(Images(Images<='1399' & Images>='1300'))
RandomMatchedImage = MatchedImages{ceil(rand(1)*length(MatchedImages))}
...
sca;
imagedata=imread(RandomMatchedImage);
win=Screen('OpenWindow',0);
black=BlackIndex(win);
Screen('FillRect', win, black);
TexturePointer=Screen('MakeTexture',win,imagedata);
clear imagedata;
Screen('DrawTexture',win,TexturePointer);
Screen('Flip',win);
WaitSecs(10);
sca;

채택된 답변

Guillaume
Guillaume 2014년 11월 6일
Unfortunately, matlab's strcmp is less functional than C's strcmp and can't really be used for the sort of comparison you want. You have several options though:
If all you want to do is group the filename by their hundred, you could just compare the first two digits of the filenames, with strncmp:
MatchedImage = Images(strncmp(Images, '13', 2)); %get all images that start with '13'
This won't work if you want something finer grained than thousand, hundred, decade, or unit. For that, you could use a regular expression and test if it match with isempty.
Finally, you could convert the numeric part of your strings into double and do the comparison based on that. You can use str2double but you need to make sure your strings are just numbers. For that a simple regex would suffice:
ImageNumbers = str2double(regexp(Images, '\d+', 'match', 'once'));
MatchedImages = Images(ImageNumbers <= 1399 & ImageNumbers >= 1300);
This latter option gives you the most flexibility.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image display and manipulation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by