필터 지우기
필터 지우기

Slicing arrays and save it in the file

조회 수: 19 (최근 30일)
Elzbieta
Elzbieta 2024년 7월 11일 11:56
댓글: Mathieu NOE 2024년 7월 16일 7:31
Hello,
I have a file of the following form:
data condition device
-297.367190000000 8 1
-295.132890000000 8 1
-268.007000000000 8 2
-268.007000000000 8 2
-262.109380000000 7 1
-263.296880000000 7 1
-263.562500000000 7 2
-263.562500000000 7 2
-258.973210000000 6 1
-255.209820000000 6 1
-255.209820000000 6 2
-255.209820000000 6 2
I would like to process the data being a slice of array for the given condition and device for instance one slice would be:
-297.367190000000 8 1
-295.132890000000 8 1
and save the result in file with the following format for instance for the data above:
dataProcessed_condition8_device1
How could I cut such slice and save it in the file of the described format?
Regards
Elzbieta

채택된 답변

Mathieu NOE
Mathieu NOE 2024년 7월 11일 13:56
hello
with your input data in attachment, try this code :
% load data
data = readmatrix('data.txt'); % or readtable if you prefer
% select condition & device
condition = 8;
device = 1;
% select rows that satisfy both conditions
ind = (data(:,2) == condition) & (data(:,3) == device);
data_out = data(ind,:); %
% export to txt file
filename_out = "dataProcessed_condition" + num2str(condition) + "_device" + num2str(device)+ ".txt";
writematrix(data_out,filename_out,"Delimiter","tab");
% edit the file
type(filename_out)
-297.36719 8 1 -295.13289 8 1

추가 답변 (1개)

Elzbieta
Elzbieta 2024년 7월 13일 10:58
Thank you a lot!

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by