cellfunと同じ​処理をtableに対​して行う方法を教えて​ください。

14 次查看(过去 30 天)
K_S_
K_S_ 2022-8-5
评论: K_S_ 2022-8-8
現在、添付ファイルのようなデータを分割して処理するためにtableをcellに変換し、処理を行っています。
出来ればtableのまま処理を行いたいのですが、何か方法はありますか?
filename = "test_cellfun.txt";
opts = detectImportOptions(filename);
opts.DataLines = [1 inf];
opts.VariableTypes = {'char', 'char'};
input_data = readtable(filename,opts);
x = table2cell(input_data(:,1));
x1 = cellfun(@(x) x(1:8),x,'UniformOutput',false);
x2 = cellfun(@(x) x(9:end),x,'UniformOutput',false);
y = table2cell(input_data(:,2));
y1 = cellfun(@(y) y(1:8),x,'UniformOutput',false);
y2 = cellfun(@(y) y(9:end),x,'UniformOutput',false);

采纳的回答

Atsushi Ueno
Atsushi Ueno 2022-8-5
> 出来ればtableのまま処理を行いたいのですが、何か方法はありますか?
cellfun関数に対してrowfun関数があります
filename = "test_cellfun.txt";
opts = detectImportOptions(filename);
opts.DataLines = [1 inf];
opts.VariableTypes = {'char', 'char'};
opts.VariableNames = {'x', 'y'}; % Warningが出るので適当な変数名を追記しました
input_data = readtable(filename,opts)
input_data = 6×2 table
x y ____________________ ____________________ {'10000000aaaaaaaa'} {'10000000aaaaaaaa'} {'1000000000000000'} {'1000000000000000'} {'ab000000ffffffff'} {'ab000000ffffffff'} {'000000001b000000'} {'000000001b000000'} {'0000000000000000'} {'0000000000000000'} {'00000000000000a1'} {'00000000000000a1'}
rowfun(@myfun,input_data,'NumOutputs',4,'OutputVariableNames',{'x1' 'x2' 'y1' 'y2'})
ans = 6×4 table
x1 x2 y1 y2 ________ ________ ________ ________ 10000000 aaaaaaaa 10000000 aaaaaaaa 10000000 00000000 10000000 00000000 ab000000 ffffffff ab000000 ffffffff 00000000 1b000000 00000000 1b000000 00000000 00000000 00000000 00000000 00000000 000000a1 00000000 000000a1
function [x1 x2 y1 y2] = myfun(x,y)
x1 = x{:}(1:8);
x2 = x{:}(9:end);
y1 = y{:}(1:8);
y2 = y{:}(9:end);
end
  1 个评论
K_S_
K_S_ 2022-8-8
ありがとうございます。大変参考になりました。

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 table 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!