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:
String in function input

Subject: String in function input

From: Claudia Champagne

Date: 14 Jan, 2010 20:36:02

Message: 1 of 3

How can I enter string inputs into a function within a loop?

I want to use the function [d,A]=strdist(r,b,krk,cas) in a loop since I have a number of r and b inputs for which I need the distance.

The problem is that inputs r and b have to appear in a string form for the function to work:
e.g. strdist('MATLAB','MathWorks',2,1)
ans = 6 9

However, if these 2 strings ('Matlab' and 'Mathworks') are located in arrays, the function does not work properly as it doesn't read them as strings.

For example:
r_array = cell array of r (where 'Matlab' is located)
b_array = cell array of b (where 'MathWorks' is located)

I tried the following :

[d,A] = strdist(cellstr(r_array{1,1}), cellstr(b_array{1,1}),2,1)

and I get an answer if 1, which is the answer that I get when I enter just about anything as input.

How can I tell the function to read my inputs as strings?

Subject: String in function input

From: Miroslav Balda

Date: 14 Jan, 2010 21:40:27

Message: 2 of 3

"Claudia Champagne" <claudia.champagne@usherbrooke.ca> wrote in message <hinv7i$afv$1@fred.mathworks.com>...
> How can I enter string inputs into a function within a loop?
>
> I want to use the function [d,A]=strdist(r,b,krk,cas) in a loop since I have a number of r and b inputs for which I need the distance.
>
> The problem is that inputs r and b have to appear in a string form for the function to work:
> e.g. strdist('MATLAB','MathWorks',2,1)
> ans = 6 9
>
> However, if these 2 strings ('Matlab' and 'Mathworks') are located in arrays, the function does not work properly as it doesn't read them as strings.
>
> For example:
> r_array = cell array of r (where 'Matlab' is located)
> b_array = cell array of b (where 'MathWorks' is located)
>
> I tried the following :
>
> [d,A] = strdist(cellstr(r_array{1,1}), cellstr(b_array{1,1}),2,1)
>
> and I get an answer if 1, which is the answer that I get when I enter just about anything as input.
>
> How can I tell the function to read my inputs as strings?

Hi,

It is necessary to test the input variables, just like:

function [d,A] = strdist(a,b,c,d)
% STRDIST ......
if ischar(a)
    r = a;
else
    r = a{:};
end
:
etc

Hope it helps

Mira

Subject: String in function input

From: Oleg Komarov

Date: 14 Jan, 2010 22:48:03

Message: 3 of 3

"Claudia Champagne"
> How can I enter string inputs into a function within a loop?
>
> I want to use the function [d,A]=strdist(r,b,krk,cas) in a loop since I have a number of r and b inputs for which I need the distance.
>
> The problem is that inputs r and b have to appear in a string form for the function to work:
> e.g. strdist('MATLAB','MathWorks',2,1)
> ans = 6 9
>
> However, if these 2 strings ('Matlab' and 'Mathworks') are located in arrays, the function does not work properly as it doesn't read them as strings.
>
> For example:
> r_array = cell array of r (where 'Matlab' is located)
> b_array = cell array of b (where 'MathWorks' is located)
>
> I tried the following :
>
> [d,A] = strdist(cellstr(r_array{1,1}), cellstr(b_array{1,1}),2,1)
>
> and I get an answer if 1, which is the answer that I get when I enter just about anything as input.
>
> How can I tell the function to read my inputs as strings?

In1 = {'Hi','Hello'};
In2 = {'Bye','GoodBye'};

nIter = numel(In1);
if nIter == numel(In2)
Out = cell(nIter,2);
for n = 1:numel(In1)
[Out{nIter,1}, Out{nIter,1}] = strdist(In1{nITer},In2{nITer},2,1);
end

Oleg

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