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!