Change or save data .txt
이전 댓글 표시
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
Azzi Abdelmalek
2014년 11월 22일
what is f?
Akmal Rahmat
2014년 11월 22일
Guillaume
2014년 11월 22일
If
a = [255 255 0 124 126]
b = [255 0 255 126 124]
What result do you expect?
Akmal Rahmat
2014년 11월 22일
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]
답변 (1개)
Image Analyst
2014년 11월 22일
편집: Image Analyst
2014년 11월 22일
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
Akmal Rahmat
2014년 11월 22일
Guillaume
2014년 11월 22일
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
2014년 11월 22일
Akmal Rahmat
2014년 11월 22일
편집: Akmal Rahmat
2014년 11월 22일
Image Analyst
2014년 11월 22일
편집: Image Analyst
2014년 11월 22일
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
2014년 11월 22일
편집: Akmal Rahmat
2014년 11월 22일
Akmal Rahmat
2014년 11월 22일
편집: Akmal Rahmat
2014년 11월 22일
Image Analyst
2014년 11월 22일
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
2014년 11월 22일
Image Analyst
2014년 11월 23일
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.
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!