PLOT ARRAY OF SURF PLOTS IN ONE PLOT
显示 更早的评论
I have mutltiple text files with x, y, and z data attached. This files are named in matrix form according to their location.
I would like to create one surf plot that combine the surf plot of the individual text files. The x, and y data can be continuous row and column numbering but the z data remain the same.
Will appreciate a help. Pls let me know if you need more info.
Thanks in advance.
采纳的回答
load() each file. Extract column 3. reshape it as 614 x 588 and then transpose it to 588 x 614. Store in a cell array according to the information from the file name.
When they are all loaded, cell2mat to get the overall Z matrix.
7 个评论
Thanks. But I am sorry. Can i get a sample code that does all the above mentioned? not really clear to me
D11 = load('1-1.txt');
D12 = load('1-2.txt');
D21 = load('2-1.txt');
D22 = load('2-2.txt');
C{1,1} = reshape(D11(:,3), 614, []).';
C{1,2} = reshape(D12(:,3), 614, []).';
C{2,1} = reshape(D21(:,3), 614, []).';
C{2,2} = reshape(D22(:,3), 614, []).';
S = cell2mat(C);
surf(S, 'edgecolor', 'none')
In the case where the files might not be exactly the size of the given ones:
D11 = load('1-1.txt'); c11 = max(D11(:,2));
D12 = load('1-2.txt'); c12 = max(D12(:,2));
D21 = load('2-1.txt'); c21 = max(D21(:,2));
D22 = load('2-2.txt'); c22 = max(D22(:,2));
C{1,1} = reshape(D11(:,3), c11, []).';
C{1,2} = reshape(D12(:,3), c12, []).';
C{2,1} = reshape(D21(:,3), c21, []).';
C{2,2} = reshape(D22(:,3), c22, []).';
S = cell2mat(C);
surf(S, 'edgecolor', 'none');
The cell2mat() step will fail if the arrays are not compatible sizes.
If the arrays are not compatible sizes, then you have to define how you want to stitch them together.
Thanks @Walter Roberson
Compatible sizes means not divible be each other right?
The second code failed for different sizes. Error below.
"Error using cat
Dimensions of arrays being concatenated are not consistent.
Error in cell2mat (line 83)
m{n} = cat(1,c{:,n});
Error in Cellarray3 (line 11)
S = cell2mat(C); "
How do i stitch incompatible array sizes together?
Compatible sizes: the number of columns of {1,1} and {2,1} must be the same as each other. The number of columns of {1,2} and {2,2} must be the same as each other. The sum of the number of rows of {1,1} and {2,1} must be the same as the sum of the number of rows of {1,2} and {2,2} .
That is, each column of the cell array is put together into a single larger column, and then once all of the columns are put together, the results are put side by side, so the total number of rows has to be the same in each case.
If that is not the case, then you need to decide how you want the portions to go together.
For example, should the maximum height of patches in a row be found, and all patches in that row should be placed at the top of an area that tall with the shorter ones being padded with zeros (which might look fine at the bottom but is probably going to look odd in-between.)
Or should the maximum height of patches in a row to be found, and all patches in that row should be centered in an array the height of the tallest one?
You should give a couple of diagrams of what you would like if the sizes are different.
I really didnt grabs fully your last 3 paragraphs. However, the fourth paragraph might be the right portioning.
I attached 4 files with incompatible sizes. Would appreciate a fix for the code. Thanks.
filename11 = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1037665/1,1.txt';
filename12 = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1037670/1,2.txt';
filename21 = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1037675/2,1.txt';
filename22 = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1037680/2,2.txt';
D11 = readmatrix(filename11); c11 = max(D11(:,2));
D12 = readmatrix(filename12); c12 = max(D12(:,2));
D21 = readmatrix(filename21); c21 = max(D21(:,2));
D22 = readmatrix(filename22); c22 = max(D22(:,2));
C{1,1} = uint8(reshape(D11(:,3), c11, [])).';
C{1,2} = uint8(reshape(D12(:,3), c12, [])).';
C{2,1} = uint8(reshape(D21(:,3), c21, [])).';
C{2,2} = uint8(reshape(D22(:,3), c22, [])).';
C
C = 2×2 cell array
{580×596 uint8} {580×608 uint8}
{588×614 uint8} {580×596 uint8}
rows = cellfun(@(M) size(M,1), C);
cols = cellfun(@(M) size(M,2), C);
targcols = max(cols, [], 1);
targrows = max(rows, [], 2);
colpadleft = cellfun(@(M) [M, zeros(size(M,1),targcols(1)-size(M,2))], C(:,1), 'uniform', 0);
colpadright = cellfun(@(M) [M, zeros(size(M,1),targcols(2)-size(M,2))], C(:,2), 'uniform', 0);
colpad = [colpadleft, colpadright];
colpad
colpad = 2×2 cell array
{580×614 uint8} {580×608 uint8}
{588×614 uint8} {580×608 uint8}
rowpadup = cellfun(@(M) [M; zeros(targrows(1)-size(M,1),size(M,2))], colpad(1,:), 'uniform', 0);
rowpaddown = cellfun(@(M) [M; zeros(targrows(2)-size(M,1),size(M,2))], colpad(2,:), 'uniform', 0);
rowpad = [rowpadup; rowpaddown];
padded = cell2mat(rowpad);
imshow(padded)

The black lines are the places the subsection was padded on the right or bottom.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
