How do I put a condition into a specific column for matlab?
17 views (last 30 days)
Show older comments
Hi all, Would like to check here on what kind of code I could use to create a specific condition on a particular column for matlab. So here is the case, I have a text file(see attached), with four columns of values in it. And here is the code for matlab as given below
A = dlmread('test1.txt')
B = A>1000
So according to this line of code, I have imported the file and placed them in a matrix A. Subsequently I set a condition for matrix B whereby A has to be of a greater value than 1000. Using this, I was able to get returns of '0' and '1's, which is what I wanted. Now, I want to create a set of conditions such that if column 1, 2 and 3 in any of the rows equals to 1, I will be able to display/print an output in that row that says 'Powergrip'. Apart from that condition, should 1,2 equals to 1 in any of the rows there would be a display in that row that says 'precisiongrip'? I do believe this has to do with a series of if else conditions but Im not exactly sure on how to go about writing this conditions. Please pardon my poor coding abilities as I'm really new to this. Thank you!
2 Comments
Stephen23
on 10 Mar 2018
Edited: Stephen23
on 10 Mar 2018
Note that your fopen and fclose are superfluous, as the file ID not used by dlmread.
Your question is not clear: "... there would be a display..." and "...I will be able to display/print an output in that row...", but where do you want to "display" this: in the command window, in a GUI, or do you want to write this to a file?
Accepted Answer
Stephen23
on 10 Mar 2018
Edited: Stephen23
on 10 Mar 2018
This should get you started, just play around with the logical conditions and character concatenation until you get what you want:
A = csvread('test1.txt');
B = A>1000;
X1 = all(B(:,1:3),2);
X2 = any(B(:,1:2),2);
C1 = {'','PowerGrip'};
C2 = {'','PrecisionGrip'};
[num2str(A),char(strcat({' '},C1(1+X1),C2(1+X2)))]
which displays this in the command window:
ans =
10 13 8 16
9 18 21 41
4 5 26 1
7 48 6 48
10 11 36 14
8 21 8 25
...
44 51 32 45
37 48 48 52
50 58 40 29
40 51 52 46
47 81 55 18
46 53 54 47
48 77 51 36
53 78 49 45
52 91 53 44
52 100 51 46
48 93 54 47
55 98 52 59
51 97 57 49
47 100 54 68
55 130 52 48
286 177 56 63
33240 56129 67683 92 PowerGripPrecisionGrip
79617 64019 67522 92 PowerGripPrecisionGrip
66281 67357 63916 97 PowerGripPrecisionGrip
71727 67721 64136 97 PowerGripPrecisionGrip
79381 69925 67599 95 PowerGripPrecisionGrip
74152 0 65272 92 PrecisionGrip
74347 0 65709 95 PrecisionGrip
82034 13 60512 94 PrecisionGrip
76831 71759 67628 91 PowerGripPrecisionGrip
79574 627 67594 95 PrecisionGrip
74439 10 65255 95 PrecisionGrip
79481 52 62760 99 PrecisionGrip
79491 24 65629 94 PrecisionGrip
79445 40 62636 88 PrecisionGrip
79705 25 68068 62 PrecisionGrip
77177 56306 67258 53 PowerGripPrecisionGrip
82089 23 68282 66 PrecisionGrip
79500 11 68059 61 PrecisionGrip
76983 14490 69494 92 PowerGripPrecisionGrip
82012 352 67713 95 PrecisionGrip
79532 23 73521 94 PrecisionGrip
89865 85677 62293 101 PowerGripPrecisionGrip
55462 5468 6932 75 PowerGripPrecisionGrip
107 216 56 56
96 188 57 48
86 157 54 53
51 111 51 33
50 117 53 45
49 108 55 26
56 106 51 44
51 104 50 36
51 106 44 47
41 107 53 45
42 102 49 28
30 97 51 49
42 105 52 6
43 85 52 49
42 103 51 29
41 99 33 44
21 104 47 45
45 104 37 28
...
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical 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!