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

6 ビュー (過去 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 Center および File Exchangetable についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!