Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Help with Matlab code

Subject: Help with Matlab code

From: Jaime

Date: 24 Sep, 2013 14:38:06

Message: 1 of 2

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!

Subject: Help with Matlab code

From: someone

Date: 24 Sep, 2013 20:52:07

Message: 2 of 2

"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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us