Convert a vector in Character array after a for loop

Hi, given the following code
n=length(x)
f = zeros(1,n)
for i = 1:n
if x(i) == 1
f(1,i) = 22;
elseif x(i)==2
f(1,i) = 23;
elseif x(i)==3
f(1,i)= 21;
elseif x(i)==4
f(1,i) = 23;
elseif x(i)==5
f(1,i)= 24;
end
end
C= char (f)
I don't know why but the 'char' conversion doesn't work giving to me the following
so with nothing inside the vector, that I need later for a switch case.
Do you know how to fix the problem?

8 件のコメント

madhan ravi
madhan ravi 2019 年 7 月 24 日
madhan ravi:
I would probably use:
str = f + ""
luca ruozi:
I need a character array and not a string, to run a switch cycle as I wrote above.
madhan ravi:
Could you illustrate how you want it to go?
luca ruozi:
n=length(x)
f = zeros(1,n)
for i = 1:n
if x(i) == 1
f(1,i) = 22;
elseif x(i)==2
f(1,i) = 23;
elseif x(i)==3
f(1,i)= 21;
elseif x(i)==4
f(1,i) = 23;
elseif x(i)==5
f(1,i)= 24;
end
end
C=char(f)
T21=3
T22=2
T23=3
T24=2
for i = 1:n
switch C
case 21
T21 = T21-1
case 22
T22 = T22-1
case 23
T23 = T23-1
case 24
T24 = T24-1
otherwise
disp ('dse')
end
end
I want then to put C inside the switch cycle
Stephen23
Stephen23 2019 年 7 月 24 日
"I don't know why but the 'char' conversion doesn't work "
Actually char works exactly as documented.
You provide the input values 22 to 24 to char, and those are the exact characters that char gives you. Here is a simple example:
>> x = randi(5,1,9)
x =
5 1 3 2 2 3 4 2 3
then run your code to get exactly the non-printable characters that you requested:
>> f =
24 22 21 23 23 21 23 23 21
>> C =
and checking that char did everything correctly:
>> double(C)
ans =
24 22 21 23 23 21 23 23 21
The fact that you requested non-printable characters does not mean that char does not work, it just means that you need to learn about character encodings:
luca
luca 2019 年 7 月 24 日
Thanks. But the mean problem is that I cannot use C inside the switch cycle. And I don't know why. May you help me?
Stephen23
Stephen23 2019 年 7 月 24 日
編集済み: Stephen23 2019 年 7 月 24 日
"But the mean problem is that I cannot use C inside the switch cycle."
Actually you can: you just need to use exactly the same non-printable (and hence invisible) characters that you defined in C. Why you want complicate your life with non-printable characters is a mystery, but MATLAB is not stopping you from doing so.
"May you help me?"
I am not sure what you want: you told char to generate some non-printable characters, and so that is exactly what you got. What do you expect to happen? What are you actually trying to achieve?
Basil C.
Basil C. 2019 年 7 月 24 日
what exactly do the values
f(1,i) = 22;
f(1,i) = 23;
f(1,i) = 21;
Represent?
luca
luca 2019 年 7 月 24 日
What do you suggest to not complicate my life?
I don't know hot to do because I'm a really beginner. May you help me?
luca
luca 2019 年 7 月 24 日
Probably Char is not the right Command.
What I want to do is. Create a vector f through the for cycle and then use this vector as switch case. The SWITCH expression must be a scalar or a character vector, but in my case f is an array with numeric value. Do you know hot to do?
Stephen23
Stephen23 2019 年 7 月 24 日
編集済み: Stephen23 2019 年 7 月 24 日
"I don't know hot to do because I'm a really beginner."
That is okay, we are here to help. But so far you have not actually described what you are trying to achieve: you have not explained how the result/output should be derived from the input, or provided example input and output arrays.
Without a specification we have to rely on guessing what you are trying to achieve.
Forget about code, what are you trying to do ?

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2019 年 7 月 24 日

0 投票

Avoiding character manipulations using basic indexing:
>> x = [1,2,3,1];
>> V = 'A':'Z';
>> V(x)
ans = ABCA

3 件のコメント

luca
luca 2019 年 7 月 24 日
THANKS TO ALL
now the cose is working.
f= [1 2 3 4 1]
V = 'A':'Z';
V(f)
n=length(f)
for i=1:n
switch V(i)
case 'A'
disp ('aser')
case 'B'
disp ('sfe')
case 'C'
disp ('df4r')
case 'D'
disp ('at34tr')
otherwise
disp ('qwert')
end
end
Thanks to both of you
luca
luca 2019 年 7 月 24 日
Sorry may I ask you one more thing?
With basic indexing I have a problem. The fourth value is 1. 1 should be always the case A. But in this case is E. and the code display qwert.
Do you know how to fix in order to have Case A also for the fourth value.
THANKS
Stephen23
Stephen23 2019 年 7 月 24 日
編集済み: Stephen23 2019 年 7 月 24 日
Instead of
switch V(i)
you need
switch V(f(i))
Otherwise you are just iterating over the elements of V, which is unlikely to be useful.

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2019 年 7 月 24 日

0 投票

Perhaps this is what you want?
>> f=[1,2,3,4], F=char(64+f)
f =
1 2 3 4
F =
'ABCD'

2 件のコメント

luca
luca 2019 年 7 月 24 日
What I want to do is Create a vector f through the for cycle and then use this vector as switch case. The SWITCH expression must be a scalar or a character vector, but in my case f is an array with numeric value. Do you know hot to do?
Matt J
Matt J 2019 年 7 月 24 日
You can loop over the elements of f,
n=length(x)
f = zeros(1,n)
for i = 1:n
switch f(i)
case 22
case 23
case 24
...
end
end
but I have a nagging feeling that a switch-case is not the best way of doing whatever it is you're trying to do.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2019a

質問済み:

2019 年 7 月 24 日

編集済み:

2019 年 7 月 24 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by