Error with struct2table command - matlab

I am getting the following error while running a program.
??? Undefined function or method 'struct2table' for input arguments of type 'struct'.
Error in ==> cellarray at 13
T=struct2table(parameter,'AsArray',true);
The program is as under
a=10;
b=15;
parameter(a).alpha_star=0;
parameter(b).gamma_star=0;
x=5;
for j=1:b
for i=1:a
parameter(i).alpha_star=x+i;
end
parameter(j).gamma_star = x^2+j;
end
T=struct2table(parameter,'AsArray',true);
Can you please tell me, where am I wrong?

I am guessing you have a matlab older than R2013b, and since do not have this function (you can check that via which struct2table).
you can try to work around it by using struct2array and reshape.
you can also use
alp=[];
gam =[];
for i=1:length(parameter)
if ~isempty(parameter(i).alpha_star)
alp(end+1)=(parameter(i).alpha_star);
end
if ~isempty(parameter(i).gamma_star)
gam(end+1)=(parameter(i).gamma_star);
end
end
to extract the values to seperate arrays and
fieldnames(parameter);
to get the fieldnames, this is dirty but if it is only needed for presentation...

Related

Using third party software within parfor loop

I wrote a set of nested for loops to conduct a parameter sweep in a third party software (Lumerical, it's an FDTD package) in Matlab. This code works perfectly well, but I can't seem to get the parallel version to work. My guess is that Matlab is having issues assigning workers in the parallel case, but I'm having issues resolving this. I've copied my code for the loop below and the line in the function I use to execute each Lumerical simulation. Does anyone have any suggestions for how to resolve this?
parfor ph = 1:PH
for p = 1:P
for bi = 1:BI
for pii = 1:PI
for r =1:R
poolobj = gcp;
addAttachedFiles(poolobj, FDTD_exec_pillars_parfor.m);
pitch = [PillarHeight(ph), Pitch(p), BackgroundIndex(bi),...
PillarIndex(pii), Radius(r)];
Transdata = FDTD_exec_pillars_parfor(pitch);
cellframe2(ph,p,bi,pii,r) = Transdata;
end
end
end
end
end
% Path used to execute simulation in FDTD package within function FDTD_exec_pillars_parfor
[x1,y1]=system('"C:\Program Files\Lumerical\FDTD\bin\fdtd-solutions.exe" -run NOMAD_script.lsf');
error message:
"Error: MATLAB cannot determine whether "FDTD_exec_pillars_parfor" refers to a function or variable. See Parallel for Loops in MATLAB, "Unambiguous Variable Names"."
This error message appeared after I added the two poolobj lines in attempt to resolve this error:
"An UndefinedFunction error was thrown on the workers for 'FDTD_exec_pillars_parfor'. This might be because the file containing 'FDTD_exec_pillars_parfor' is not accessible on the workers. Use addAttachedFiles(pool, files) to specify the required files to be attached. See the documentation for 'parallel.Pool/addAttachedFiles' for more details.
Caused by: Undefined function 'FDTD_exec_pillars_parfor' for input arguments of type 'double'"

matlab function not working surprisingly

I have created this function :
function Calcul_Constantes ( xBin1 , xTin1 , xHin1 )
% computes global variables that change depending on the circumstances
global v rhoc K1v K2v xBin xTin xHin cBin cTin cHin;
xBin=xBin1;
xTin=xTin1;
xHin=xHin1;
rhoc = 0.02777*(2.106*xHin+78.12*xBin)*(6.935*xHin+23.15*xBin);
K1v=0.6*rhoc/175;
K2v=(2.70803*10^-4+7.5*10^-4*v*rhoc)/175;
cBin=0.02777*xBin;
cTin=0.02777*xTin;
cHin=0.02777*xHin;
end
and when I do test in my main script :
Calcul_Constantes(0,0,1);
xBin
xHin
the following error occures:
Error using Calcul_Constantes
Too many input arguments.
Error in Mercredi15_main (line 48)
Calcul_Constantes(0,0,1);
I'd be grateful for any help, I really can't see what does not work
Probably there is another Calcul_Constantes function somewhere else. You might have saved another version of Calcul_Constantes function somewhere.
In command line type :
which Calcul_Constantes
and check if the return directory and .m file is the which you are trying to use. Rename or delete the wrong function.

Matlab 'main' function isn't reading local functions

I have a code in matlab (~1000 lines) that constists of about 15 functions. The code runs fine with each function as a different script, but I want to put them all into one file so I can use the publish function more easily. However, when I put them all together my 'main' function isn't recognizing the local functions. Here's what it looks like:
function full_function()
...
values = fvalues(0);
...
end
function values = fvalues(state)
...
end
When I try to run it, it gives me
"Undefined function 'fvalues' for input arguments
of type 'double'.
Error in full_function (line 32)
values = fvalues(0);"
I've looked all over for how to do local functions and I have no idea what I'm doing wrong. If I right-click on fvalues and hit 'open' it even brings me to the correct portion of the code, so I have no idea why full_function cannot read it. Please help! Thanks.

Matlab postset method sample fails

I'm trying to call a function right after I change some properties of an object in Matlab. I went to the documentation and found this sample code:
classdef PropListener < handle
properties (SetObservable, AbortSet)
PropOne
PropTwo
end
methods
function obj = PropListener(evtobj)
if nargin > 0
addlistener(evtobj,'PropOne','PostSet',#PropListener.handlePropEvents);
addlistener(evtobj,'PropTwo','PostSet',#PropListener.handlePropEvents);
end
end
end
methods (Static)
function handlePropEvents(src,evnt)
switch src.Name
case 'PropOne'
display('I was here!');
case 'PropTwo'
display('I was here too!');
end
end
end
end
I saved this into a file, went to the console and tryed
test1=PropListener
test1.PropOne=1
and it doesn't show the "I was here!" message. The rest works perfectly.
After some debuging, I found 2 problems:
1 - It calls the constructor method without arguments, therefore
if(argin>0)
returns always false and it never creates the listeners.
2 - If I remove the if/end structure, when I call the constructor
test1 = PropListener
Matlab returns "Error using PropListener (line 9)
Not enough input arguments."
(line 9 is the line with the following statement)
addlistener(evtobj,'PropOne','PostSet',#PropListener.handlePropEvents);
Now, if I remove the whole constructor method and call
addlistener(evtobj,'PropOne','PostSet',#PropListener.handlePropEvents);
on the command line, everything works, but I would have to remember to call that line while writing my scripts, and I don't want to run that risk.
I'd like to know how could I implement this postset feature, and any help would be greatly appreciated.
Thanks in advance,

an error in Matlab: Input argument undefined, although variable is existing

I have written this function, and I have already defined values for rg and Lp, but still when I run this function it returns the error : (Input argument "Lr" is undefined.
Error in ==> Bis at 12
if f(Lr,rg,Xo)*f(Lr,rg,Xf)>0)
here is the function :
function[Lp,Xo,Xf]=Bis(Lr,rg)
Xo=0;
Xf=10;
Err=0.01;
syms x;
f=inline('(sqrt((2/3)*(((x*Lr)/3)-(x*x)+((2*x*x*x)/Lr)-((2*x*x*x*x)/(Lr*Lr))+(((2*x*x*x*x)/(Lr*Lr))*exp(-Lr/x))))-rg)');
if f(Lr,rg,Xo)*f(Lr,rg,Xf)>0
disp('The values you entered are not apropriate !')
PlotLpFunction;
Lp='unknown';
elseif f(Lr,rg,Xo)*f(Lr,rg,Xf)==0
if f(Lr,rg,Xo)==0
Lp=Xo;
elseif f(Lr,rg,Xf)==0
Lp=Xf;
end
elseif f(Lr,rg,Xo)*f(Lr,rg,Xf)<0
xi=(Xf-Xo)/2;
while abs(f(Lr,rg,xi))>Err
if f(Lr,rg,xi)*f(Lr,rg,Xf)<0
Xo=xi;
xi=(Xo+Xf)/2;
elseif f(Lr,rg,xi)*f(Lr,rg,Xf)>0
Xf=xi;
xi=(Xo+Xf)/2;
end
end
Lp=xi;
end
The code executes for me on the newest version of Matlab, other than the fact that I don't have the PlotLpFunction.
My initial impression was that you forgot to send the Lr (and all other argument) into you're inlined f function, very easy to fix by adding them as arguments to the inline function. You'll find the full usage in the official documentation.
The relevant part being
inline(expr,arg1,arg2,...) constructs an inline function whose input
arguments are specified by the strings arg1, arg2,.... Multicharacter
symbol names may be used.
but it seems to form the inline just fine by itself on both Matlab 2011b and 2008b, from context presumably. Answer is accepted now, so presumably that was the problem. Can anyone else reproduce his problem? If so please provide your Matlab version or other circumstances.