How do I import a matrix into Matlab and then visualize it as a surface?
I want to have it something like this at the end:
http://www.mathworks.se/help/matlab/ref/meshgrid.html
to be able to do that I have to first have it as an input of the meshgrid(according to the file) but I have no idea how to do that.
enter link description here
I am completely new in Matlab...
Thanks in advance
The are many possibilities (file formats, visualisation functions, etc) depending on what you would like to achieve. The simplest example I can think of is as follows.
Suppose you have a file named data.txt in your working directory that contains
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
Then the commands
M = load('data.txt');
surf(M)
xlabel('x')
ylabel('y')
title('Matrix M')
will give you the following plot
Since the matrix M is not a square matrix, you can see in the plot which dimension is assigned to each axis.
To change the viewpoint you can use the view command. All there is to this command is summarised in this picture
taken from here http://www.mathworks.com/help/matlab/visualize/setting-the-viewpoint-with-azimuth-and-elevation.html
The first argument to be passed to the view command is the azimuth and the second argument is the elevation, as defined in the picture above.
For example, if you want to make the order of the values on the x and y axes appear reversed, you can first read the current azimuth and elevation
% get from current axes the attribute View
current_view = get(gca,'View');
and change it with view(current_view + [180 0]). The result is
You can also rotate a plot interactively: on the toolbar of the Figure window there is a circular arrow. You can click on it to activate it and then click and drag inside the window.
Related
I'm new to Julia and I used MNIST handwritten digit train data to get multiple images in matrix with size 28 x 28. Let's assume I store them in array img[i] with length n(n is dynamic). I want to show Images in one window such that every image has its own specific label under it.
I tried to search and read documents, currently I use hcat(images_window, img[i]) for all images and plot(images_window) and annotate some text label for each image in specific coordinates. This way is not a good practice and n is not configurable either.
I expect Julia have something like dynamic layout for its plots and I can show Image in each subplot and show them in a window with something like this:
plt = plot()
for (i, subplot) in enumerate(plot)
plot!(plt, subplot, layout(i))
end
display(plt)
You didn't mention which plotting library you are using but from the basic syntax I'm vaguely guessing that you might be asking bout Plots.jl.
In Plots, plotting multiple subplots on one figure in principle works like this:
using Plots
p1 = plot(rand(5))
p2 = plot(rand(5))
plot(p1, p2)
i.e., you call plot with multiple arguments which themselves are plots. You can then additionally specify a layout kwarg, which in its simplest form takes a tuple of (nrows, ncols) and places the subplots in a grid with the specified number of rows and columns.
As an example, here's three plots next to each other:
plot(plot.([rand(5) for _ ∈ 1:3])..., layout = (1, 3))
This question already has an answer here:
How to create an array according to row and column number [duplicate]
(1 answer)
Closed 5 years ago.
The problem is the following: I have a .txt file containing 3 columns of numbers. The first 2 columns are the coordinate x,y of the points. The third columnn (z vector) is made of numbers that express the luminosity of each point. (The .txt files have been generated by a software that is used to study the pictures of a combustion process). Each vector (x,y,z) is made of 316920 elements (all integer numbers). Now: is there a way to create from these 3 vectors an image in matlab relating the luminosity value to the coordiantes of the point?
Thanks for your time!
consider a file image.txt contains y x and intensity values separated line. like this.
1 1 0
1 2 12
1 3 10
....
....
255 255 0
open the text file using fopen function
fid = fopen(image.txt,'r');
im=[];
and read a string-line of characters by fgetl function, convert string-line into vector using sscanf and put intensity value into y and x coordinates of a image matrix, im.
tline=fgetl(fid) ;
rd=sscanf(tline,'%d');
im(rd(1),rd(2))=rd(3);
The same process is iterated up-to end of file.
at last close file-handle fid
I am going to assume that the three columns in your text file are comma separated( The code will need to be a bit different if they are not comma separated) . Since you said all numbers are integers, I am going to assume that you have all the data needed to fill a 2D grid using your x and y coloumns is present. I am not assuming that it is in a ordered form. With these assumptions the code will look like
data = csvread(filename)
for i=1:length(data)
matrix(data(i,2)+1,data(i,1)+1)=data(i,3) // +1 is added since there maybe a index starting from 0 and matlab needs them to start from 1
end
image(matrix)
For other delimiters use
data = dlmread(filename,delimiter)
As written in the manual of Gnuplot at page 136,
Because the pm3d algorithm does not extend the colored surface outside
the range of the input data points, the 'c' coloring options will
result in pixels along two edges of the grid not contributing to the
color of any quadrangle. For example, applying the pm3d algorithm to
the 4x4 grid of data points in script demo/pm3d.dem (please have a
look) produces only (4-1)x(4-1)=9 colored rectangles.
So if I would like to plot a 4x4 pixel sized image, I can't do it in this way without missing the 4th column and row. (I would like not to modify the file.) Is there an efficent workaround for this problem?
To plot a 4x4 image which shows exactly the color rectangles specified in the data file, the splot ... with pm3d is not suited. For this one can use the image plotting style. The pm3d mode must be used e.g when the grid points are not equidistant, or for a 3D representation.
Taking the following file data.dat
1 6 8 3
2 5 4 4
9 1 1 2
5 4 3 8
a more or less minimal script would be
set autoscale fix
set xtics 1 out
set ytics 1 out
plot 'data.dat' matrix with image t ''
The result for set terminal pngcairo is:
The terminal must support plotting with image, this is the case for the most, but not for all (e.g. pstricks does not). In that case one would need to use with image failsafe.
I have a file that contains the map between the two entities. I have to plot the x and and Y axis in a graph using linux. The X axis is Time like (12:35:07) and the Y axis will have some integer numbers range (1-14). I need to plot the x and the Y axis using some method that should be called from linux shell script. X axis will have totally around 500 to 800 samples. say (from 12:14:00 to 12:30:00). Can anyone please help me out. I tried using GNUplot, but the graph is not proper. Sample input is given below
12:34:58 5
12:35:06 9
12:35:07 14
12:35:07 13
12:35:08 4
12:35:08 5
12:35:17 9
12:35:17 13
12:35:18 14
12:35:19 4
12:35:19 5
This is what I have written
1 set terminal png
2 set output 'test.png'
3 set xdata time
4 set timefmt "%H:%M:%S"
5 set yrange [1:15]
6 plot "Graph1" using 1:2 title 'data A'
The problem I have is since the image has large number of samples (around 700 intervals, I am not able to see each value properly. Also the Y axis is not proper. I need to restrict the Y axis to onlyh 10 values (1-10). Also I need to draw 4 similar graphs and place each graph in each corner (subplot). I have not done the subplot yet in GNUplot.
It seems that you have three main issues right now. For some of them the issue is not totally clear, so it would help if you could update your question (or make a comment) to clarify, and I can update my response.
1) Not able to see each of ~700 values properly.
Here I need a bit of clarification. I'm assuming you don't want to see each of 700 data points individually, rather you want to be able to get rid of clutter on the x axis which happens when plotting time values. For this you can use the command
set xtics X
where X is the interval between tics you want, in this case a number of seconds. Is this what you want?
You can also use
set format x '%M:%S'
(or something) if you want to control how the x values are displayed on the axis.
2) y axis needs to be restricted to 10 values.
Here also it's not totally clear what you are looking for. Do you want to scale everything down to be between 1 and 10? The range of your data presented is 4 to 14, so by subtracting four you can get things between 0 and 10 like this:
plot "Graph1" using 1:($2-4) title 'data A'
If you want to constrict an arbitrary y data set to be between 1 and 10, that's a little more tricky:
stats "Graph1"
plot "Graph1" using 1:(($2-STATS_min_y)*9/(STATS_max_y-STATS_min_y)+1) title 'data A'
The stats command gets statistics about a file before you plot it. The convoluted plot command should scale everything to be between 1 and 10 on the y axis.
3) You want to have 4 subplots.
This one is pretty easy. Before your first plot command, use the command
set multiplot layout 2,2
This will create a 2x2 grid for your plots. Every plot command will plot on a new subplot. Type help set multiplot at the gnuplot command line for details on changing the sequence of subplots used.
I need to write an application in VB6 which makes a scatter plot out of a series of data points.
The current workflow:
User inputs info.
A bunch of calculations go down.
The output data is displayed in a series of 10 list boxes.
Each time the "calculate" button is clicked, 2 to 9 entries are entered into the list boxes.
One list box contains x coordinates.
One list box contains the y coordinates.
I need to:
Scan through those list boxes, and select my x's and y's.
Another list box field will change from time to time, varying between 0 and 100, and that field is what needs to differentiate which series on the eventual graph the x's and y's go into. So I will have Series 1 with six (x,y) data points, Series 26 with six data points, Series 99 with six data points, etc. Or eight data points. Or two data points. The user controls how many x's there are.
Ideally, I'll have a graph with multiple series displaying all this info.
I am not allowed to use a 3rd party solution (e.g. Excel). This all has to be contained in a VB6 application.
I'm currently trying to do this with MS Chart, as there seems to be the most documentation for that. However, this seems to focus on pie charts and other unrelated visualizations.
I'm totally open to using MS Graph but I don't know the tool and can't find good documentation.
A 2D array is, I think, a no go, since it would need to be of a constantly dynamically changing size, and that can't be done (or so I've been told). I would ideally cull through the runs, sort the data by that third series parameter, and then plug in the x's and y's, but I'm finding the commands and structure for MS Chart to be so dense that I'm just running around in very small circles.
Edit: It would probably help if you can visualize what my data looks like. (S for series, made up numbers.)
S X Y
1 0 1000000
1 2 500000
1 4 250000
1 6 100000
2 0 1000000
2 2 6500
2 4 5444
2 6 1111
I don't know MSGraph, but I'm sure there is some sort of canvas element in VB6 which you can use to easily draw dots yourself. Scatter plots are an easy graph to make on your own, so long as you don't need to calculate a line of best fit.
I would suggest looking into the canvas element and doing it by hand if you can't find a tool that does it for you.
Conclusion: MSChart and MSGraph can both go suck a lemon. I toiled and toiled and got a whole pile of nothing out of either one. I know they can do scatter plots, but I sure as heck can't make them do 'em well.
#BlackBear! After finding out that my predecessor had the same problems and just used Pset and Line to make some really impressive graphs, I did the same thing - even if it's not reproducible and generic in the future as was desired. The solution that works, albeit less functionally >> the solution with great functionality that exists only in myth.
If anyone is reading this down the line and has an actual answer about scatter plots and MSChart/Graph, I'd still love to know.