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

14 views (last 30 days)
K_S_
K_S_ on 5 Aug 2022
Commented: K_S_ on 8 Aug 2022
現在、添付ファイルのようなデータを分割して処理するために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);

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 5 Aug 2022
> 出来れば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 Comment
K_S_
K_S_ on 8 Aug 2022
ありがとうございます。大変参考になりました。

Sign in to comment.

More Answers (0)

Categories

Find more on table in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!