フィルターのクリア

How to convert string to array

114 ビュー (過去 30 日間)
mor zarfati
mor zarfati 2017 年 4 月 23 日
コメント済み: Shayla Lussiné 2023 年 3 月 31 日
I have a list of names in a string variable ( for example "'a','b','c'") and i need to convert it to an array [a;b;c]. is there a way to do this? thank you!
  1 件のコメント
Stephen23
Stephen23 2017 年 4 月 23 日
編集済み: Stephen23 2017 年 4 月 23 日
If you are trying to access variables dynamically using string names, then read this:

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

回答 (4 件)

amine&&
amine&& 2017 年 4 月 23 日
Hello, you can see the documentation for str2num() in https://fr.mathworks.com/help/matlab/ref/str2num.html.

mor zarfati
mor zarfati 2017 年 4 月 23 日
hi, my variables are not numbers, they are gene names so i cannot convert to numbers. thank you!
  1 件のコメント
amine&&
amine&& 2017 年 4 月 23 日
Hello, you can see this function cell2mat() in https://fr.mathworks.com/help/matlab/ref/cell2mat.html .

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


Image Analyst
Image Analyst 2017 年 4 月 23 日
編集済み: Image Analyst 2017 年 4 月 23 日
Use char:
% Define cell array of strings:
ca = {'a','b','c', 'demo', 'long string'}
% Make character array
charArray = char(ca)
Shows:
ca =
1×5 cell array
'a' 'b' 'c' 'demo' 'long string'
charArray =
a
b
c
demo
long string

Shayla Lussiné
Shayla Lussiné 2023 年 3 月 31 日
移動済み: Stephen23 2023 年 3 月 31 日
It is not necessary to convert it in a array 'cuz when you enter the text, this is already an array of characters. For example, if you enter text = 'hello', when you put text(1) the result will be 'h'. It's just like with the numbers.
  2 件のコメント
Image Analyst
Image Analyst 2023 年 3 月 31 日
編集済み: Image Analyst 2023 年 3 月 31 日
text is a built in function and should not be used for variable names. I think what you meant was
plot((1 : 5), randi(8, 1, 5), 'b.-', 'LineWidth', 2);
grid on;
str = 'Hello Shayla';
x = 2;
y = 5; % Whatever.
text(x, y, str, 'FontSize', 30, 'FontWeight', 'bold', 'Color', 'r');
text(x, 4, str(1), 'FontSize', 30, 'FontWeight', 'bold', 'Color', 'm');
Shayla Lussiné
Shayla Lussiné 2023 年 3 月 31 日
I didn't knew that, and thanks for the correction. What I mean was if you insert a string of characters in a variable and if you want to use one of the letters you just simply type the position. Like
my_variable='abc'
%in case "abc" then just char(my_variable)
my_variable(1)

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by