Slicing arrays and save it in the file

5 views (last 30 days)
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

Accepted Answer

Mathieu NOE
Mathieu NOE on 11 Jul 2024
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

More Answers (1)

Elzbieta
Elzbieta on 13 Jul 2024
Thank you a lot!

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!