how to plot data from a csv in matlab - matlab

I have a csv file with the following data structure:
p1_1,p2_1,p3_1
p1_2,p2_2,p3_2
p1_3,p2_3,p3_3
and I want to plot P2 againt P3 in matlab,
I wrote this code:
function plotData
dbstop if error
fileName='C:\\Temp\\out100-2.csv';
m=csvread(fileName);
plot(m(2),m(3));
but the plot is empty. I checked and m has data, so it is the way that I am using plot is not right.
How can I fix the problem so I can plot it?

m(2) and m(3) are just two scalar values, hence you only plot a single point.
You need to supply vectors to plot, e.g.:
plot(m(:,1),m(:,2))
this plots the data from the first and second columns of your csv file.

In your plot command
plot(m(2),m(3))
you are only plotting a single point. Perhaps you meant to plot to column vectors
plot(m(:,2), m(:,3))

Related

How to convert double to object handle to graph a contour plot?

I am working with app designer in MATLAB where I have the user input a bunch of parameters and push a button and then generate a contour plot. The app first uses dlmread to save data from 3 separate files to the workspace. Then what the goal is, is to generate a corresponding contour plot on that same GUI that uses data from those 3 files (my x, y, and z parameters).
However, when I run the program, I get an error that says:
"Error setting property 'HetroTransSpec' of class 'Parameters':
Cannot convert double value 5 to a handle"
app.HetroTransSpec is the name of my contour plot. Parameters is the name of my GUI application. I will present the code:
function SetParametersButtonPushed(app, event)
spec = dlmread('/Users/******/MATLAB/SphoHetroTest/Spec3.txt'); %Load spec file
assignin('base', 'spec', spec);
pop=dlmread('/Users/******/MATLAB/SphoHetroTest/pop.txt');
assignin('base', 'pop', pop);
lambda=dlmread('/Users/******/MATLAB/SphoHetroTest/lambda.txt'); %read wavelenght axis (nm)
assignin('base', 'lambda', lambda);
Now, here is my code to take these parameters (spec, pop, lambda) to generate my contour plot. Except I am getting that error:
app.HetroTransSpec = contourf(pop,lambda,spec);
Any help would be greatly appreciated!
The result of a contour plot is not a handle, but contour data, as opposed to some other plotting functions.
Try:
[~,handle]= contourf(pop,lambda,spec);
app.HetroTransSpec =handle;

Plotting 3 vectors in Matlab GUI axes handle

I am trying to plot 3 vectors onto matlab GUI in a serial object's callback.
I want to plot this on axes handle but the problem is it only plot last vector;
plot(handles.axes1,sensor1,'r');
plot(handles.axes1,sensor2,'b');
plot(handles.axes1,sensor3,'g');
I searched on internet and find that this issue can be solved with hold on and hold of feature so I tried this
plot(handles.axes1,sensor1,'r');
hold on ;
plot(handles.axes1,sensor2,'b');
plot(handles.axes1,sensor3,'g');
hold off;
but in this case a new figure is opened(dont know why) and again only the last plot is drawn.
I am stucked. If any one have idea of what would be the issue?
Thanks
I'm not sure why your first try using "hold" didn't work. Seems like it should have.
But in any case, you can get the desired behavior in a single command:
plot(handles.axes1,length(sensor1),sensor1,'r',...
length(sensor2),sensor2,'b',...
length(sensor3),sensor3,'g');
This specifies both an X = length(sensor_) and a Y = sensor_ to the plot command. When you only give plot a Y input, it assumes an X of length(Y). But you can't combine multiple traces in a single plot command by giving only the Y input for each, because it will try to treat the inputs as X,Y pairs.
As the vectors are the same length we can simply combine them as the columns of a matrix and then plot the matrix
plot(handles.axes1,[sensor1',sensor2',sensor3'])
However these will have the default colour order. Without specifying x values setting colors within the plot command is tricky. However (luckily) the default order starts:
blue,green,red...
so swapping the column order will plot the lines with the colours requested
plot(handles.axes1,[sensor2',sensor3',sensor1'])
(this assumes the vectors are rows, if they are columns don't transpose them)

Matlab get an average plot out of several plot

I'm just getting started with matlab and I'm trying to plot some graph with it.
The problem is I don't know how to get the average data out of 10 plot().
Can anyone guide me for it? Thank you :)
Assuming you don't have access to the original data you used for doing the plots:
plot_data = get(get(gca,'Children'),'YData'); % cell array of all "y" data of plots
average = mean(cell2mat(plot_data));
In order for this to work, you have to use this code right after doing the plots, that is, without plotting to any other figure (gca is a handle to the current axes).
Assume your data is stored row-wise in a m x n matrix A, with n columns corresponding to different values of the continuous error, and m rows corresponding to different curves. Then to inspect the mean over the curves just use
Amean = mean(A,1);
plot(Amean)
Please take a look at this link: it solve my problem getting the average plot.
https://www.mathworks.com/matlabcentral/fileexchange/27134-plot-average-line
After downloading the files just put those script on your working folder and add this line to your script.
plotAverage(gca,[],'userobustmean',0)

Plotting data structured (x,y,value)

I am trying to plot a dataset using imagesc in Matlab.
The dataset is structured like this:
x1 y1 value1
x2 y2 value2
x3 y3 value3
...
The problem:
when I try to plot it like this:
imagesc(x,y,value)
the figure is only in one dimension.
It works well when I plot it with plot3, using the values for the z-axis.
How can I visualize this dataset using imagesc?
imagesc needs a matrix structure rather than the 3 vector you mentioned, and assumes that the data is used in uniform space grids. So I'd use scatter instead to begin with. A way to still use imagesc is to interpolate to an uniform grid and construct a matrix out of the 3 vectors you have:
If you want to convert your non-uniform data the function you are looking for is griddata.
It handles the interpolation and returns a matrix of values.
This can be plotted by imagesc, surf or whatever.
scatter is usually the better way, but that depends on your application.
Try looking the source code of imagesc function. You can see how it is made. To see it, write:
edit imagesc

Matlab : Plot each line of a matrix as a function of its index

I would like to create a plot with several lines, each corresponding to a row at a given matrix.
To be more elaborate, I have got a matrix M where each row represents a value that is changing along the columns. I would like to plot this change as a function of the column index to each of the rows, so to plot (e.g) the first rowI should :
plot(M(1,:));
The thing is, I would like to plot all the rows. Of course I could iterate over them, hold and plot the current one:
(plot(M(i,:))
but I'm wondering if there's a simple command or a single-liner that would do it.
I have tried plotmatrix but without much success regarding the desirable results.
Thanks!
Have you tried plot(M')?
From the first paragraph of the documentation of plot:
plot(Y) plots the columns of Y versus the index of each value when Y is a real number.