필터 지우기
필터 지우기

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

조회 수: 3 (최근 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!