フィルターのクリア

Plotting a grid of filled squares from true/false values in MATLAB

5 ビュー (過去 30 日間)
P
P 2013 年 11 月 27 日
コメント済み: Walter Roberson 2013 年 11 月 28 日
I want to create a grid of equal squares in MATLAB. I have a matrix which stores mixed values of 1 or a 0. I want the 1's to represent black squares and the 0's to be white with a white outline encapsulating the black squares for clearness/black grid lines for white boxes.
There is no need for units or values on either axis (i'm representing some shaded/unshaded PV panels).
The pseudocode is something like:
For(x row of variable, y column of variable)
If (variablename(x,y)) value = 1
draw a black square with white outline
elseif (variablename(x,y)) value = 0
draw a white square with black outline
end
end

回答 (2 件)

Walter Roberson
Walter Roberson 2013 年 11 月 28 日
Something like this (untested)
[sx, sy] = size(YourLogicalMatrix);
rectangle([sx/2 sy/2 1/2 1/2], 'facecolor', 'b');
[x, y] = find(YourLogicalMatrix);
for K = 1 : length(x)
rectangle([x(K)-1/2, Y(K)-1/2, 1, 1], 'Linewidth', 2, 'EdgeColor', 'w');
end
  4 件のコメント
P
P 2013 年 11 月 28 日
編集済み: P 2013 年 11 月 28 日
Am I correcting or adding those lines? I tried both. Adding them after the end of the loop yields:
??? Undefined function or method 'Y' for input arguments of type 'double'.
And a blue coloured rectangle appears with no grid lines.
Correcting lines 2 and 5 with the new code yields the same blue box and the same error message.
To test it I did
wallshading = ones(11,6)
wallshading(1:8) = 0
And then ran the script with wallshading as the variable. Thanks.
Walter Roberson
Walter Roberson 2013 年 11 月 28 日
[sx, sy] = size(YourLogicalMatrix);
rectangle('Position', [sx/2 sy/2 1/2 1/2], 'facecolor', 'b');
[x, y] = find(YourLogicalMatrix);
for K = 1 : length(x)
rectangle('Position', [x(K)-1/2, y(K)-1/2, 1, 1], 'Linewidth', 2, 'EdgeColor', 'w');
end

サインインしてコメントする。


P
P 2013 年 11 月 28 日
tried
imagesc(wallshading);
colormap(gray);
grid on;
how can resize the squares to fit the dimensions and get the grid lines to be black for the white squares and vice versa? (it will look a little bit like a checkerboard)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by