Output with specific critiria in data ?

I am having a txt file with 4 columns. I would like to get a new file with spesific critiria, depending on the limits of values of 1st and 2nd columns. I would like my first column to have values ranging from a (minimum value) to b (maximum value). Also I would like my 2nd column to gave values ranging from c (minimum value) to d (maximum value). After that I want to get a file that would have all the rows that will have a<1st column values<b & c<2nd column values<d.
I am uploading a file as an example. (For example I would like to include values that:
2<1st column<18
30<2nd column<100)
Could you help me?
Thank you in advance

6 Comments

What have you tried so far? This doesn't seem hard to achieve using logical indexing.
I know how to make it in unix. But its difficult to make it. I thing if statement could help me but I did not make it.
I thing:
if a<first<b && c<second<d
....
but I can not syntax it
mask = a<first & first<b & c<second & second<d;
selected = big(mask,:);
writematrix(selected, 'OutputFile.txt', 'delimiter', '\t');
Excuse me, If I have three more columns with stringshow could I keep them also with the specific numbers?
I am uploading the new file also in order to understand what I mean.
I tried
But no use.
clc
clear
filename1= 'big.txt' %arxeio me makroseismika
[d1,tex]= xlsread(filename1);
n=d1(:,1);
m=d1(:,2);
%a, b are numbers
mask = a<n & n<b & c<m & m<d;
selected = d1(mask,:), tex(mask,:);
t=tex(mask,:);
writematrix(selected, 'OutputFile.xlsx');
Could you please help me?
selected = [num2cell(d1(mask,:)), tex(mask,:)];
writecell(selected, 'OutputFile.xlsx');
Excuse me but in my output file , only the tex are shown! The data (I mean numbers) are not shown. Specifically NaN is shown.
Could you help me to fix it?

Sign in to comment.

 Accepted Answer

%need some numbers to test with
a = 3; b = 20;
c = 5; d = 100;
%okay, do the work
filename1 = 'big.txt'; %arxeio me makroseismika
T = readtable(filename1, 'readvariablenames', false);
n = T{:,4};
m = T{:,5};
%a, b are numbers
mask = a<n & n<b & c<m & m<d;
selected = T(mask,:);
writetable(selected, 'OutputFile.xlsx', 'writevariablenames', false);

4 Comments

I am affraid that your code is not working. In the output file I get also variables out of "mask" limitation (eg two values habe n<a etc)....
please post your a b c d values so that I can replicate the problem. To confirm, you are still using the same big.txt file you posted?
Excuse me, I use the following code and for some reason I get this message
clc
clear
filename2= 'all_.xlsx'
d2=readtable(filename2,'readvariablenames', false);
Number_of_OBS=d2{2:end,27};
maskk = Number_of_OBS>1
selected = d2(maskk,:);
writetable(selected, 'OutputFile1_FROM1.xlsx', 'writevariablenames', false);
Undefined operator '>' for input arguments of type 'cell'.
Could you please help me?
That suggests that your 27th column was read in as text rather than as numeric.
You did not happen to mention your release, and you did not post a sample all_.xlsx file for us to test with.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!