格式化单元格加倍
Formatting cell to double
我有一个文本文件,其中的数据类似于 (1,2),(3,4),1 (4,5),(6,7),1
我正在读取 matlab 中的文件:
A= textread('abc.txt', '%s');
输出是
A: 2x1 cell
(1,2),(3,4),1
(4,5),(6,7),1
任何人都请帮助将此单元格转换为双输出
New_A= 5x2 double
1 2 3 4 1
4 5 6 7 1
对于您的特定示例,阅读文件后,您可以使用 textscan。
A = {'(1,2),(3,4),1'
'(4,5),(6,7),1'};
New_A = cell2mat(cellfun(@(line) cell2mat(textscan(line,'(%f,%f),(%f,%f),%f')), A, 'UniformOutput', 0));
New_A =
1 2 3 4 1
4 5 6 7 1
您也可以逐行执行类似操作,因为它们是从文件中读取的。
这可以使用 regexprep
, cell2mat
and str2num
完成,如下所示:
A = {'(1,2),(3,4),1'
'(4,5),(6,7),1'}; % This is what you get after this: A= textread('abc.txt', '%s');
New_A = regexprep(A,'(\(|\))',''); %Removing the brackets ()
New_A = str2num(cell2mat(New_A)) %Converting char cell to numeric matrix
我有一个文本文件,其中的数据类似于 (1,2),(3,4),1 (4,5),(6,7),1 我正在读取 matlab 中的文件:
A= textread('abc.txt', '%s');
输出是
A: 2x1 cell
(1,2),(3,4),1
(4,5),(6,7),1
任何人都请帮助将此单元格转换为双输出
New_A= 5x2 double
1 2 3 4 1
4 5 6 7 1
对于您的特定示例,阅读文件后,您可以使用 textscan。
A = {'(1,2),(3,4),1'
'(4,5),(6,7),1'};
New_A = cell2mat(cellfun(@(line) cell2mat(textscan(line,'(%f,%f),(%f,%f),%f')), A, 'UniformOutput', 0));
New_A =
1 2 3 4 1
4 5 6 7 1
您也可以逐行执行类似操作,因为它们是从文件中读取的。
这可以使用 regexprep
, cell2mat
and str2num
完成,如下所示:
A = {'(1,2),(3,4),1'
'(4,5),(6,7),1'}; % This is what you get after this: A= textread('abc.txt', '%s');
New_A = regexprep(A,'(\(|\))',''); %Removing the brackets ()
New_A = str2num(cell2mat(New_A)) %Converting char cell to numeric matrix