我编写了一个matlab代码,它将加载写在文本文件中的矩阵,然后我想将其显示为图像。 文本文件以随机方式包含从0到2的整数,我想用不同的颜色表示每个,例如0表示白色,1表示某种颜色,2表示不同颜色。 我将在下面提供matlab代码:
clc; clear all; for i=1:10 k=num2str(i); f = strcat('test_file_num_',k,'.txt'); a{i} = fileread(f); [m,n] = size(a{i}); a{i} = reshape(a{i},12,10); a{i} = a{i}'; a{i} = a{i}(:,1:10); end文件夹中有10个文本文件,每个文件包含一个包含整数0到2的随机矩阵,每个文本文件的名称以“test_file_num_”开头,而在{i}中我的矩阵为10x10矩阵。 现在我想将a {i}矩阵表示为图像或图形或任何东西,但要有一些能够以不同颜色显示矩阵内容的东西。 感谢你的帮助。
I have written a matlab code which would load the matrix written in a text file and then I want to show it as an image. The text file contains integers number from 0 to 2 in a random fashion and I want to represent each in a different colors e.g. 0 in white,1 in some color and 2 in a different color. I would provide the matlab code just below :
clc; clear all; for i=1:10 k=num2str(i); f = strcat('test_file_num_',k,'.txt'); a{i} = fileread(f); [m,n] = size(a{i}); a{i} = reshape(a{i},12,10); a{i} = a{i}'; a{i} = a{i}(:,1:10); endThere are 10 text file in the folder each of which contain a random matrix containing integer numbers 0 to 2 and name of each text file starts with "test_file_num_" and in a{i} I have the matrix which is 10x10 matrix. Now I want to represent the a{i} matrix as an image or graph or anything but to have something which would show the contents the matrix in a different color. Thanks for all your help.
最满意答案
如果文件中的数据是结构化的,意味着数字之间有标签或空格,您可以直接使用importdata将数据加载到矩阵中。 之后,您可以使用imagesc生成图像。 要指定颜色,可以使用色彩colormap功能更改颜色colormap 。 所以你的代码看起来像这样:
% Example data A = floor(3*rand(10)); % Change colormap with just 3 colors (Red,Green,Blue) cmap = [1 0 0;0 1 0;0 0 1]; figure; imagesc(A); colormap(cmap); colorbar;希望有所帮助!
If the data in your file is structured, meaning has tabs or spaces between the numbers you can directly use importdata to load your data into a matrix. After that, you can use imagesc to produce the image. To assign the colors you can change the colormap using the colormap function. So your code would look something like this:
% Example data A = floor(3*rand(10)); % Change colormap with just 3 colors (Red,Green,Blue) cmap = [1 0 0;0 1 0;0 0 1]; figure; imagesc(A); colormap(cmap); colorbar;Hope that helps!
更多推荐
发布评论