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:
concatenate string and string matrix

Subject: concatenate string and string matrix

From: Xianfeng

Date: 29 Jul, 2014 23:34:06

Message: 1 of 9

Hi:
Does anyone encounter this problem? I simply cannot figure out how to do it.
I have one string matrix.

A=['aa';'bb'];

I want to combine string '@(x)' with string matrix A to form the string.

B='@(x)['aa';'bb']';

then I can use str2func(B) to generate C=@(x)['aa';'bb'].

Does anyone know how to do it? I already try strcat or vertcat or horzcat. It doesn't work. The previous example is my simplified matrix. My real string matrix is pretty large.

thanks

shi

Subject: concatenate string and string matrix

From: Nasser M. Abbasi

Date: 30 Jul, 2014 00:02:57

Message: 2 of 9

On 7/29/2014 6:34 PM, Xianfeng wrote:
> Hi:
> Does anyone encounter this problem? I simply cannot figure out how to do it.
> I have one string matrix.
>
> A=['aa';'bb'];
>
> I want to combine string '@(x)' with string matrix A to form the string.
>
> B='@(x)['aa';'bb']';
>
> then I can use str2func(B) to generate C=@(x)['aa';'bb'].
>
> Does anyone know how to do it? I already try strcat or vertcat or horzcat.
It doesn't work. The previous example is my simplified matrix. My real string matrix is pretty large.
>
> thanks
>
> shi
>

how about:

['x' A(1,:)]

xaa

['x' A(2,:)]

xbb

now you can make a function of it.

--Nasser

Subject: concatenate string and string matrix

From: Xianfeng

Date: 30 Jul, 2014 14:58:10

Message: 3 of 9

Dear Nasser:
Thanks a lot for your suggestion. I try your solution. It is a little different from my expection.
Your method make each row in string Matrix A to be one function handle. But I want to have the form @(x)[aa;bb;cc;......]. I have total 5000 row strings. each row string size is 1000. I only want to return one function with one handle name. It is similar to this one: fh =@(x)['abcdef...';'ghikmn...';'k...';]. do you have any suggestion?
thanks

shi

Subject: concatenate string and string matrix

From: Steven Lord

Date: 30 Jul, 2014 16:09:47

Message: 4 of 9


"Xianfeng " <xfshi2000@gmail.com> wrote in message
news:lrb162$c0n$1@newscl01ah.mathworks.com...
> Dear Nasser:
> Thanks a lot for your suggestion. I try your solution. It is a little
> different from my expection.
> Your method make each row in string Matrix A to be one function handle.
> But I want to have the form @(x)[aa;bb;cc;......]. I have total 5000 row
> strings. each row string size is 1000. I only want to return one function
> with one handle name. It is similar to this one: fh
> =@(x)['abcdef...';'ghikmn...';'k...';]. do you have any suggestion?

So you want your function handle to just return the string matrix stored in
A? That's easy.

f = @(x) A;

The anonymous function f will "remember" A and return it even if you clear A
or pass f into a function where A is not defined or is defined to be
something other than your original A.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: concatenate string and string matrix

From: Xianfeng

Date: 30 Jul, 2014 16:34:07

Message: 5 of 9

Dear Steven:
Thanks for your quick answer. Sorry I may not clearly state my question. I have a very complicated function. this function consists of multiple rows equation. x is variable. For example, my ideal function handle can be like this:
fh=@(x)[x(1)^3+x(2)^4;
x(3)^2+x(4)^4;
x(5)^2+x(6)^6]
Now I know how to generate string matrix A=['x(1)^3+x(2)^4';
             'x(3)^2+x(4)^4';
             'x(5)^2+x(6)^6'].
My goal is to define a function handle to matrix A. Then when I can call fh(x) with x=[1 2 3 4 5 6], it will return
fh([1 2 3 4 5 6])

ans =

          17
         265
       46681

If I directly define fh=@(x) A; then call fh([1 2 3 4 5 6]), it returns
 fh2=@(x) A;
K>> fh2([1 2 3 4 5 6])

ans =

x(1)^3+x(2)^4
x(3)^2+x(4)^4
x(5)^2+x(6)^6

this is not want, I want x variable is substituted into the equation and show results. thanks

shi

Subject: concatenate string and string matrix

From: matt dash

Date: 30 Jul, 2014 17:20:23

Message: 6 of 9

"Xianfeng" wrote in message <lrb6pv$rdv$1@newscl01ah.mathworks.com>...
> Dear Steven:
> Thanks for your quick answer. Sorry I may not clearly state my question. I have a very complicated function. this function consists of multiple rows equation. x is variable. For example, my ideal function handle can be like this:
> fh=@(x)[x(1)^3+x(2)^4;
> x(3)^2+x(4)^4;
> x(5)^2+x(6)^6]
> Now I know how to generate string matrix A=['x(1)^3+x(2)^4';
> 'x(3)^2+x(4)^4';
> 'x(5)^2+x(6)^6'].
> My goal is to define a function handle to matrix A. Then when I can call fh(x) with x=[1 2 3 4 5 6], it will return
> fh([1 2 3 4 5 6])
>
> ans =
>
> 17
> 265
> 46681
>
> If I directly define fh=@(x) A; then call fh([1 2 3 4 5 6]), it returns
> fh2=@(x) A;
> K>> fh2([1 2 3 4 5 6])
>
> ans =
>
> x(1)^3+x(2)^4
> x(3)^2+x(4)^4
> x(5)^2+x(6)^6
>
> this is not want, I want x variable is substituted into the equation and show results. thanks
>
> shi

You want fh = eval(['@(x)',A]).

But A should be a 1D row vector, not a multi-row array. A should be A = '[x(1)^3+x(2)^4;x(3)^2+x(4)^4;x(5)^2+x(6)^6]', not quotes around each "row" of the equation.

Subject: concatenate string and string matrix

From: Xianfeng

Date: 30 Jul, 2014 18:59:11

Message: 7 of 9

Dear matt:
Thank you very much for your helpful suggestion. I cannot explicitly write my matrix like A='[x(1)^3+x(2)^4;x(3)^2+x(4)^4;x(5)^2+x(6)^6]'. equation for each row is a summation of several subfunction. The number of subfunction is varied according to different experiment condition (which is user defined). the first row of A matrix is like
f1(x)+f2(x)+f3(x)+...+f20(x). fn(x) may have different formula. I have to use string to express each subfunction and combine them to form one final equation for each row.
A=['final_equation_row_1';'final_equation_row_2';....]. I have total 5000 rows in A matrix. Manually type-in is not possible. thanks

shi
 

Subject: concatenate string and string matrix

From: Bruno Luong

Date: 30 Jul, 2014 19:11:11

Message: 8 of 9

Using strings to make program is rearely a good idea

Bruno

Subject: concatenate string and string matrix

From: Xianfeng

Date: 30 Jul, 2014 19:52:11

Message: 9 of 9

Dear All
Thank you all for your sincere help. Finally I borrow matt's idea to reconstruct 1D string array. Although it is super long 1D string, it woks. I understand strings is not good way to make program. But I have to do so due to some limitations. Anyway thank you all again

shi

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