come to convert words into cell array

2 ビュー (過去 30 日間)
Luca Re
Luca Re 2024 年 10 月 21 日
編集済み: Stephen23 2024 年 10 月 21 日
g='Eb=3;bb=5;'
g = 'Eb=3;bb=5;'
i want to create arraycell as similar
S={'Eb',3;'bb',5}
S = 2x2 cell array
{'Eb'} {[3]} {'bb'} {[5]}
(';' is used to insert a new symbol

採用された回答

Stephen23
Stephen23 2024 年 10 月 21 日
編集済み: Stephen23 2024 年 10 月 21 日
g = 'Eb=3;bb=5;';
C = textscan(g,'%s%f', 'Delimiter','=', 'EndOfLine',';')
C = 1x2 cell array
{2x1 cell} {2x1 double}
C = [C{1},num2cell(C{2})] % not the best way to store this data
C = 2x2 cell array
{'Eb'} {[3]} {'bb'} {[5]}

その他の回答 (1 件)

Manish
Manish 2024 年 10 月 21 日
編集済み: Manish 2024 年 10 月 21 日
Hi,
I understand that you want to convert the variable‘ginto a cell array‘S.
You can achieve this by splitting the string using‘strsplit’ functionand adding the result to the cell array.
Here is the code implementation:
g = 'Eb=3;bb=5;';
parts = strsplit(g, ';');
S = {};
for i = 1:length(parts)-1
varValue = strsplit(parts{i}, '=');
% Add to cell array
S{i, 1} = varValue{1};
S{i, 2} = str2double(varValue{2});
end
disp(S);
{'Eb'} {[3]} {'bb'} {[5]}
Here is the documentation link for ‘strsplit’:
Hope this solves!

カテゴリ

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