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

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;

Related

MATLAB plotting function not enough input arguments

I am currently taking Andrew Ng's Stanford Machine learning course and am using MATLAB to complete the programming assignments despite being new to the MATLAB programming language as the course requires completion of assignments in either Ocatave or MATLAB. For the first programming assignment the course offers Octave/MATLAB script that steps you through the exercises. I am attempting to write a MATLAB function that will plot data for x and y variables, however, am continuing to run into the same error and am beginning to spin my wheels with finding a solution. I have a plotData.m file I am working off of and here is the code I have put together:
function plotData(x, y)
%PLOTDATA Plots the data points x and y into a new figure
% PLOTDATA(x,y) plots the data points and gives the figure axes labels of
% population and profit.
figure; % open a new figure window
hold on;
% ====================== YOUR CODE HERE ======================
% Instructions: Plot the training data into a figure using the
% "figure" and "plot" commands. Set the axes labels using
% the "xlabel" and "ylabel" commands. Assume the
% population and revenue data have been passed in
% as the x and y arguments of this function.
%
% Hint: You can use the 'rx' option with plot to have the markers
% appear as red crosses. Furthermore, you can make the
% markers larger by using plot(..., 'rx', 'MarkerSize', 10);
plot(x, y, 'rx');
xlabel('population (in tens of thousands)');
ylabel('profit (in $10,000s)');
hold off;
% ============================================================
end
Every time I run this script I receive an error stating 'Not enough input arguments. Error in plotData (line 19) plot(x, y, 'rx');'. This is a fairly ambiguous error message and I am not sure how to interpret what is wrong here. I don't see why I wouldn't have enough input arguments in this example as the function explicitly takes both x and y and uses each to plot data as defined by the function. Any help would be greatly appreciated.
I am using MATLAB version 9.6.0.1114505 (R2019a) Update 2
I was getting the same error on running 'plotData.m',
Please try running 'ex1.m' and not 'plotData.m'

Matlab: Plotting on same figure using 2 different functions

I have written a function of the form
function myplot(x,y)
plot(x,y)
end
This function creates a plot for given values of x and y. The actual function is more complex, but it does not serve the purpose of the question to include its content here. The main point follows.
I have tried to run the following script:
x = [1:0.01:10]
y = [1:0.01:10]
figure
plot(sin([1:0.01:10]))
hold on
myplot(x,y)
The intent here is to plot 2 sets of data on the same graph. The first set of data is generated by Matlab's native plot command while the second set of data is generated by the user custom myplot function (in this case what should be a straight line). The script above wont do it....
How to get Matlab to include both sets of data on the same plot?
your script plots them both but with different x values. if you don't specify x input in plot it uses 1:length(y), while your myplot function does specify x values (which in your case are 10-fold smaller).
just do: plot(x,sin([1:0.01:10])) instead of plot(sin([1:0.01:10]))
You could save the current axes (on which you create the first plot) in a variable and pass it as an argument to your function to be sure it gets plot on the same axes no matter what happens elsewhere in your code.
So your main code could look like this:
x = [1:0.01:10];
y = [1:0.01:10];
figure
plot(sin([1:0.01:10]))
hold on
%// Save axes in variable
CurrentAxes = gca;
%// Pass it as argument to function
myplot(x,y,CurrentAxes)
and the function:
function myplot(x,y,hAxes)
plot(hAxes,x,y);
end

how to plot data from a csv in 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))

How to export fitted curve to 1D vector

With the use of Curve Fitting toolbox I'm fitting to 11 data points a curve described by a custom equation. As a result I get something like this:
I want to save 1D vector represented bye the red line on the plot above into a matlab variable. I try to use Fit->Save to Workspace... option from the Curve Fitting toolbox menu, but saved variables do not contain any of the fitted data.
How can I save fitted data into matlab variable?
The saved MATLAB-object (default name is fittedmodel) contains the fitted function as a function-handle and the fitted coefficients corresponding to this function-handle. You can then evaluate at the data points of your choice with feval.
In the following example the fitted function will be evaluated at the original datapoints x:
y = feval(fittedmodel, x);
Now you can directly plot the result:
plot(x,y);

plotting 2 variable of different size in matlab

Am trying to plot 2 variable of different size length in matlab GUI using push button,
but because the variables are of different length it will not work,is there a way i can make it to plot.
d= pdist([x,y,z],'euclidean') ; % value of my distance
dd= 1:10:d; % interval and end 'd' value
FSL=-120; %value of free space loss get from the GUI
DFSL= 1:10:FSL %interval and end at FSL value
plot(dd,DFSL)
The plot code didnt work coming back with an error "
Error using plot
Vectors must be the same lengths"
You can plot vectors of two different lengths, but not against each other. You have used the syntax
plot(x,y)
which means for every element in vector x, there should be a corresponding element in vector y. In your case, you do not have this, hence the error.
You can plot like this though:
plot(x)
figure;
plot(y)
If you are looking to plot them in a single plot, subplot will be useful.