Change or save data .txt
Show older comments
a = dlmread('test4.txt');
b = dlmread('test5.txt');
if (a-b)>125
c = (a-b)>125;
c = 0;
c = dlmmwrite('test4.txt','delimiter');
c = dlmwrite('test5.txt','delimiter');
end
hello all. i need help.here is my code. how do i change the data in my .txt file ? when a-b meet my condition i want to change it in both .txt file. i keep getting error. please help me. thank you
5 Comments
Azzi Abdelmalek
on 22 Nov 2014
what is f?
Akmal Rahmat
on 22 Nov 2014
Guillaume
on 22 Nov 2014
If
a = [255 255 0 124 126]
b = [255 0 255 126 124]
What result do you expect?
Akmal Rahmat
on 22 Nov 2014
If you'd answered the question I asked we would be much closer to helping you. So, once again, if
a = [255 255 0 124 126]
b = [255 0 255 126 124]
What final a and b do you expect? Bearing in mind that:
(a-b) == [0 255 -255 -2 2]
Answers (1)
Image Analyst
on 22 Nov 2014
Edited: Image Analyst
on 22 Nov 2014
Try this (untested because you didn't attach your text files):
a = dlmread('test4.txt');
b = dlmread('test5.txt');
c =(a-b)>125; % Logical array
a(c) = 0; % Replace elements in a with 0;
b(c) = 0; % Replace elements in b with 0;
% Write updated a and b back out.
dlmwrite('test4.txt', a);
dlmwrite('test5.txt', b);

10 Comments
Akmal Rahmat
on 22 Nov 2014
Guillaume
on 22 Nov 2014
it doesn't work is not an helpful comment. What happens and what did you expect to happen? We can't read your mind.
Akmal Rahmat
on 22 Nov 2014
Akmal Rahmat
on 22 Nov 2014
Edited: Akmal Rahmat
on 22 Nov 2014
Image Analyst
on 22 Nov 2014
Edited: Image Analyst
on 22 Nov 2014
Akmal, there are no array elements where a is more than 125 greater than b. That's why "it didn't work.". Here, try this code
clc;
clearvars;
close all;
workspace;
fontSize = 33;
a = dlmread('test4.txt');
b = dlmread('test5.txt');
c =(a-b)>125; % Logical array
subplot(2,3,1);
imshow(uint8(a));
title('a', 'FontSize', fontSize);
subplot(2,3,2);
imshow(uint8(b));
title('b', 'FontSize', fontSize);
subplot(2,3,3);
imshow(c, []);
title('c', 'FontSize', fontSize);
a(c) = 0; % Replace elements in a with 0;
b(c) = 0; % Replace elements in b with 0;
subplot(2,3,4);
imshow(uint8(a));
title('New a', 'FontSize', fontSize);
subplot(2,3,5);
imshow(uint8(b));
title('New b', 'FontSize', fontSize);
% Write updated a and b back out.
% dlmwrite('test4.txt', a);
% dlmwrite('test5.txt', b);
and observe this result:

Note how there are no white pixels in c? That means the condition of a-b>125 was never true for any pixel. Perhaps you're using the wrong test4.txt.
Akmal Rahmat
on 22 Nov 2014
Edited: Akmal Rahmat
on 22 Nov 2014
Akmal Rahmat
on 22 Nov 2014
Edited: Akmal Rahmat
on 22 Nov 2014
Image Analyst
on 22 Nov 2014
Well, now a and b are different sizes, and you've introduced a "d". Also you forgot to attach 'img2.jpg'. But anyway, the additional code you posted in no way explains why you think the values in test4 should be 125 greater than in test5.
Finally, the unneeded statement
[x,y] = size(d);
is decpetive because x is usually the horizontal dimension but here you have it as the number of rows, which is the vertical dimension. It should be one of these:
[y, y] = size(d); % or
[rows, columns] = size(d);
But that really has nothing to do with why a is not greater than b by 125.
Akmal Rahmat
on 22 Nov 2014
Image Analyst
on 23 Nov 2014
Use my code in the comment, but have C be this:
c = a ~= b; % Logical array
This will calculate where a and b are different, like you asked for.
Categories
Find more on Data Type Conversion 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!