How to plot a parabola by getting its parameters from the gui using MATLAB - matlab

I've tried to do it like this:
I've found this https://www.quora.com/How-do-I-draw-a-parabola-in-MATLAB and tried to use it like that:
a=str2double(get(handles.InputA,'string'));
b=str2double(get(handles.InputB,'string'));
c=str2double(get(handles.InputC,'string'));
xLine=[(-b)/2*a-5:0.01:(-b)/2*a+5];
yToPlot= a*x.^2 + b.x+c;
plot(xLine,yToPlot);
but I keep getting errors...any help would be appreciated

You define the variable xLine, but use the variable x in yToPlot. That's why you get an error saying x is not defined. Also in yToPlot you have b.x. MATLAB then thinks that b is a struct and that you want to access the field named x of b. Since b is not a struct, and doesn't have a field x, you'll get the error 'Attempt to reference field of non-structure array.'. If you fix these two, it should work, according to the code you've given:
xLine=[(-b)/2*a-5:0.01:(-b)/2*a+5];
yToPlot= a*xLine.^2 + b*xLine+c;
plot(xLine,yToPlot);

Related

How can I resolve the "need struct type but got struct"

As you can see on my picture, I have a column named probability and I want to create a new column from the probability column. I want to extract values from the probability column which is an array. But while trying to do so, I receive an error:
"Can't extract value from probability#52427: need struct type but got struct<type:tinyint,size:int,indices:array<int>,values:array<double>>"
Here is my extraction code:
preds_test = preds.withColumn("newCol", col("probability").getItem(3))
Can someone please tell me what I did wrong?
I figured it out. I used a lambda function. This is my code:
preds_subset = preds.select('CustomerID','prediction', probs_churn('probability')).orderBy(asc("probability"))```

JES: Implementing modification code to a Loop

I am using JES and am wondering what built in function I should use to make this effect work.
newG=(oldG+(abs(x*y*2.57901)%64))%256
So far I have this code
def forLoop():
picture = makeEmptyPicture(300,200)
show(picture)
for p in getPixels(picture):
setColor(p,black)
repaint(picture)
for p in getPixels(picture):
oldG=(p)
newG=(oldG+(abs(x*y*2.57901)%64))%256
repaint(picture)
The error I get is
The error was:x Name not found globally.
A local or global name could not be found. You need to define the function or variable before you try to use it in any way.
you'll need to define the local names for x and y to be able to get the newG color, hope this helps
coding is something like this:
x= getX(p)
y= getY(p)

Matlab: How to remove the error of non-existent field

I am getting an error when running matlab code. Here I am trying to use one of the outputs of previous code as input to my new code.
??? Reference to non-existent field 'y1'.
Can anyone help me?
A good practice might be to check if the field exists before accessing it:
if isfield( s, 'y1' )
% s.y1 exists - you may access it
s.y1
else
% s.y1 does not exist - what are you going to do about it?
end
To take Edric's comment into account, another possible way is
try
% access y1
s.y1
catch em
% verify that the error indeed stems from non-existant field
if strcmp(em.identifier, 'MATLAB:nonExistentField')
fprintf(1, 'field y1 does not exist...\n');
else
throw( em ); % different error - handle by caller?
end
end
Have you used the command load to load data from file(s)?
if yes, this function overwrite your current variables, therefore, they become non-existent, so when you call, it instead of using:
load ('filename');
use:
f=load ('filename');
now, to refer to any variable inside the loaded file use f.varname, for
example if there is a network called net saved within the loaded data you may use it like:
a = f.net(fv);
I would first explain my situation and then give the solution.
I first save a variable op, it is a struct , its name is coef.mat;
I load this variable using coef = load( file_path, '-mat');
In a new function, I pass variable coef to it as a parameter, at here, the error Reference to non-existent field pops out.
My solution:
Just replace coef with coef.op, then pass it to the function, it will work.
So, I think the reason behind is that: the struct was saved as a variable, when you use load and want to acess the origin variable, you need point it out directly using dot(.) operation, you can directly open the variable in Matlab workspace and find out what it wraps inside the variable.
In your case, if your the outputs of previous code is a struct(It's my guess, but you haven't pointed out) and you saved it as MyStruct, you load it as MyInput = load(MyStruct), then when use it as function's parameter, it should be MyInput.y1.
Hops it would work!
At first load it on command window and observe the workspace window. You can see the structure name. It will work by accessing structure name. Example:
lm=load('data.mat');
disp(lm.SAMPLE.X);
Here SAMPLE is the structure name and X is a member of the structure

MATLAB: Referencing an element in a structure

I am trying to reference an element buried within a structure that I did not create (hence I don't know the exact way in which it was built).
Having loaded the structure, if I type:
dataFile.RECORDINGS.eye
I receive the following output:
ans =
2
ans =
2
Both of those variables will always be the same, but they could be at any time 1, 2 or 3. What I'd like to do is check with a switch statement which looks like this:
switch dataFile.RECORDINGS.eye
case {1, 2}
% action A
case 3
% action B
end
Of course, the above throws up an error because 'case' cannot check whether dataFile.RECORDINGS.eye contains a given value since there are two elements stored under that address. So, my question is: how do I reference just one of the elements? I thought it would be as simple as replacing the first line with:
switch dataFile.RECORDINGS.eye(1)
...But, this gives the error:
??? Field reference for multiple structure elements that is followed by more reference blocks is an error.
Similarly, I can't access the element like this:
switch dataFile.RECORDINGS.eye.1
...As I get the following error:
??? Dot name reference on non-scalar structure.
If the values are really always the same, you can try the following to get a scalar that can be used in the switch command:
unique([dataFile.RECORDINGS.eye])
By the way, did you try to index RECORDINGS, i.e.,
dataFile.RECORDINGS(1).eye
dataFile.RECORDINGS(2).eye
Perhaps instead of eye having multiple elements, you have multiple elements of RECORDINGS that each have a single value of eye? You might want dataFile.RECORDINGS(1).eye or dataFile.RECORDINGS(2).eye.

??? Attempt to reference field of non-structure array. Error

So far from what I have read this error can be caused by confusing or redundant naming within the program but I don't think that is the issue here since everything is declared clearly. From what I can see in this my issue is coming from the declaration of piecewise that is then being run through integration below and therefor the program is attempting to access a array cell that doesn't exist. If this is the case I have so far been stumped at how to fix this issue. Any assistance with this problem would be greatly appreciated.
syms t k n
fct = #(t)evalin(symengine,['subs(piecewise([0 <= t and t < 2,',...
'sin((Pi*t^2)/4)],[t <= 2 and t < 3, 5*t-t^2-6], [t <=3 and t < 4, 0],',...
'[Otherwise, t-4]),t=',regexprep(mat2str(x),' ',','),')']);
evalin(symengine,'assume(k,Type::Integer)');
a = #(fct,t,k) int(fct*cos(k*pi*t/4)/4,t,-2,8);
b = #(fct,t,k) int(fct*sin(k*pi*t/4)/4,t,-2,8);
FourierSeries = #(fct,t,n) a(fct,t,0)/4 + ...
symsum(a(fct,t,k)*cos(k*pi*t/4) + b(fct,t,k)*sin(k*pi*t/4),k,1,n);
pretty(FourierSeries(t,25,1))
ezplot(FourierSeries(t,25,1),-2,8)
hold on
ezplot(fct,-2,8)
hold off
title('Partial sum with n=25')
The complete error text is as follows:
??? Attempt to reference field of non-structure array.
Error in ==> sym.int at 56
r = mupadmex('symobj::intdef',f.s,x.s,a.s,b.s);
Error in ==> #(fct,t,k)int(fct*cos(k*pi*t/4)/4,t,-2,8)
Error in ==>
#(fct,t,n)a(fct,t,0)/4+symsum(a(fct,t,k)*cos(k*pi*t/4)+b(fct,t,k)*sin(k*pi*t/4),k,1,n)
Error in ==> FourierProgram at 16 pretty(FourierSeries(t,25,1))
This was asked long ago, but I'll provide an answer since one was never given.
As your error indicates, the issue is with this line and how the anonymous function a is called:
a = #(fct,t,k) int(fct*cos(k*pi*t/4)/4,t,-2,8);
The sym/int function expects the second argument (the variable with which the integration is performed with respect to) to be a symbolic variable. However, you're calling FourierSeries(t,25,1), which passes the value 25 as the integration variable.
This bit of code should replicate the issue in your version of Matlab (back in 2011 when this was asked):
syms t k;
int(t*cos(k*pi*t/4)/4,25,-2,8)
However, in R2015a I now get a different (and a bit clearer) error message:
Cannot integrate with respect to ''. The integration variable must be a symbolic variable.