String to Array Matrix (Help)

1 回表示 (過去 30 日間)
Elsen
Elsen 2015 年 3 月 16 日
コメント済み: Elsen 2015 年 3 月 16 日
I have some problems to covert string into array matrix. For example : I have a matrix
A=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15]
And then, i have an input string/char as change array matrix such that y='1:2:3,1:4'. If we calculate manual, then the results are a matrix x=[1 2 3 4;11 12 13 14]. However, there are some problems when i want to insert string y='1:2:3,1:4' as array matrix. For example : I have a variable x, and then i will calculate matrix contents such as x=A(y). (There is some error). I was trying with cellstr, etc. It didn't work. Can you help me ? I will apply this concept into my gui matlab. Thank you for your help.
  2 件のコメント
Jos (10584)
Jos (10584) 2015 年 3 月 16 日
How are A, x and y related? And why is y a string?
Elsen
Elsen 2015 年 3 月 16 日
Thank you for your respon. I will describe you why they are correlated. I had this code in my gui matlab : input = get(edit1.handles,'String'); x=data(input); I will use input parameter for taking some array matrix that I wanne be used. Before, I created matrix data, but the problem is input (sstring). Thanknyou

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

採用された回答

Guillaume
Guillaume 2015 年 3 月 16 日
Note: input is a built-in function of matlab so avoid using that as a variable name. It's not a good variable name anyway as it does not tell you anything about the purpose of the variable (they're all either input, output or temporaries).
I'm not sure the way you're designing this is a good idea in the first place, but a way to parse and use your string would be:
A = [1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15];
Aindexstr = '1:2:3, 1:4';
Aindex = cellfun(@str2num, strsplit(Aindexstr, ','), 'UniformOutput', false);
newA = A(Aindex{:})
This uses expansion of cell arrays into comma separated list to index A using the cell array Aindex.
  1 件のコメント
Elsen
Elsen 2015 年 3 月 16 日
It's great. Thank you for your help

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by