Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Help with Matlab code
Date: Tue, 24 Sep 2013 20:52:07 +0000 (UTC)
Organization: Mitre Corp
Lines: 60
Message-ID: <l1su1n$jqr$1@newscl01ah.mathworks.com>
References: <l1s84e$4r4$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: rubyext-06-ls.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1380055927 20315 172.20.102.182 (24 Sep 2013 20:52:07 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 24 Sep 2013 20:52:07 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2318
Xref: news.mathworks.com comp.soft-sys.matlab:802869

"Jaime " <jgalasso225@yahoo.com> wrote in message <l1s84e$4r4$1@newscl01ah.mathworks.com>...
> Hello, I am trying gain some experience with Matlab and have been given some examples.  I am very stuck on this and was wondering if anyone could help.  
> 
> Here is the Mfile:
> 
> function MisprintWordM(stringToDisplay, misprintElements)
> % Presents the string in the first argument in the centre of the screen
> % The second argument (e.g. [1,3,6]) defines elements that will be
> % replaced by randomly chosen letters.
> 
> theAlphabet = 'abcdefghijklmnopqrstuvwxyz';
> % Replace elements as specified
> for iReplace = 1:length(misprintElements)
>     elementToReplace = misprintElements(iReplace);
>     newElement = ChooseRandomIntegerInRange(1, length(theAlphabet));
>     stringToDisplay(elementToReplace) = theAlphabet(newElement);
> end
> % Draw the stimulus
> close all
> rgbTriplet = [ 0 0.1 0.7 ];
> text('Position',[0.3 0.5],'String',stringToDisplay,'Color',rgbTriplet,'FontSize',20,'HorizontalAlignment','center');
> 
> % Show the stimulus until a key is pressed and then clear the display
> 
> pause;
> 
> clf;
> 
> %--------------------------------------------------------------------------
> 
> %--------------------------------------------------------------------------
> 
> function randomInteger = ChooseRandomIntegerInRange(lowerLimit, upperLimit)
> % Returns a random integer in the range lowerLimit to upperLimit
> 
> lowerFloat = lowerLimit - 0.5;
> upperFloat = upperLimit + 0.499999;
> 
> randomInteger = round(lowerFloat + (upperFloat-lowerFloat)*rand(1,1));  
> 
> %--------------------------------------------------------------------------
> 
> %--------------------------------------------------------------------------
> 
> The function defined in the file MisprintWordM.m accepts two arguments. One is used to specify a string and the second is an array that is used to specify elements of the string that should be replaced by randomly chosen letters.  I need to improve the function by testing that none of the elements specified in the second argument are invalid (e.g. that they are too high or too low to refer to elements of the string specified by the first argument). Any help with this would be greatly appreciated!

% If I understand what you are asking, 
% you simply need to ensure that the numbers in 
 % misprintElements are between 1 and length(stringToDisplay)

doc any

% something like (before your for loop):

if any(misprintElements < 1)
 % insert error message or routine here
end
if any(misprintElements > length(stringToDisplay))
   % insert error message or routine here
end