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:
How do I convert to a character?

Subject: How do I convert to a character?

From: Masood Sadiq

Date: 13 Jan, 2009 12:33:02

Message: 1 of 3

How do I convert a FOR loop index variable, i, into a character?

I tried char(i), but this didn't work. Please see (or run, if you want) the script below.
Ultimately, I wish to replace the hard-coded '/1' in the 'add_line' commands, with PortNumber.

Can anyone help? Thanks
Masood
------------
open_system(new_system('modelname'))

InputList= {'Input1', 'Input2'};
OutputList={'Output1', 'Output2'};

for i = 1:1
    InString = ['modelname/',char(InputList(i))];
    OutString = ['modelname/',char(OutputList(i))];
    PortNumber = ['/',char(i)];

    add_block('built-in/Inport', InString , 'Position', [20 55 60 60]);
    add_block('built-in/Outport', OutString, 'Position', [200 55 240 60]);

    % I want to use PortNumber instead of '/1' in the line below...
    add_line('modelname',[char(InputList(1)),'/1'],[char(OutputList(1)),'/1']);
end
save_system('modelname');
---------------------------

Subject: How do I convert to a character?

From: Yuri Geshelin

Date: 13 Jan, 2009 12:50:18

Message: 2 of 3

"Masood Sadiq" <masood.sadiq@power.alstom.com> wrote in message <gki1lu$p0f$1@fred.mathworks.com>...
> How do I convert a FOR loop index variable, i, into a character?
>
> I tried char(i), but this didn't work. Please see (or run, if you want) the script below.
> Ultimately, I wish to replace the hard-coded '/1' in the 'add_line' commands, with PortNumber.
>
> Can anyone help? Thanks
> Masood
> ------------
> open_system(new_system('modelname'))
>
> InputList= {'Input1', 'Input2'};
> OutputList={'Output1', 'Output2'};
>
> for i = 1:1
> InString = ['modelname/',char(InputList(i))];
> OutString = ['modelname/',char(OutputList(i))];
> PortNumber = ['/',char(i)];
>
> add_block('built-in/Inport', InString , 'Position', [20 55 60 60]);
> add_block('built-in/Outport', OutString, 'Position', [200 55 240 60]);
>
> % I want to use PortNumber instead of '/1' in the line below...
> add_line('modelname',[char(InputList(1)),'/1'],[char(OutputList(1)),'/1']);
> end
> save_system('modelname');
> ---------------------------

just a quick glance at your code: char(i) won't work. Use num2str(i) instead.

Subject: How do I convert to a character?

From: Masood Sadiq

Date: 13 Jan, 2009 13:12:03

Message: 3 of 3

That works fine. Thanks for the help, Yuri.
Cheers
"Yuri Geshelin" <geshelin@hotmail.com> wrote in message <gki2ma$104
> just a quick glance at your code: char(i) won't work. Use num2str(i) instead.

Tags for 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