Matlab 'main' function isn't reading local functions - matlab

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.

Related

Is there a solution to matlab claiming insufficient input arguments when enough are being supplied?

I have a custom function I have made, which requires two input arguments. This is the opening line of the function, clearly showing it needs just two input arugments:
function [file,frame,vertices,edges,faces,faceOrders,edgeOrders] = FOLD_reader(filename,rundocfolder)
The FOLD_reader function is called within another function (FEAfromFOLDVaryingStiffness), using the following line of code:
[~,frame,vertices,~,faces,~,~] = FOLD_reader(filename,rundocfolder);
To which matlab claims:
Not enough input arguments.
Error in FEAfromFOLDVaryingStiffness (line 12)
[~,frame,vertices,~,faces,~,~] = FOLD_reader(filename,rundocfolder);"
However, if I copy and paste the offending line into the command window, it works perfectly. The filename and rundocfolder variables are definitely defined in FEAFromFoldVaryingStiffness which calls FOLD_reader, as they are among the input arguments of the FEAFromFold(etc) function itself.
Has anyone had any experience with this seemingly bizzarre error? To me it makes no sense at all.
If it's a help here are the lines up to the error point inside FEAfromFOLDVaryingStiffness:
function [] = FEAfromFOLDVaryingStiffness(filename,meshsize,displacement,m,n,stiffnessvary,rundocfolder)
%Comments ommitted for brevity
[~,frame,vertices,~,faces,~,~] = FOLD_reader(filename,rundocfolder);
I'm an idiot, I called the FEAfromFOLDVaryingStiffness with one too few arguments, I'd left out m or n or something and so the last input variable (rundocfolder) was undefined, which showed up as an error with fold_reader inside the FEAfromFOLDVaryingStiffness function, instead of where the error actually was: the top level script.

linIt function in Octave

I try to convert these matlab scripts to octave. However in getGroundTruthBoxes.m, it has following code:
freq = cell2mat(accumarray(inst(inst>0), segm(inst>0), [], #(x){linIt(histc(x,1:numClass))'}, {zeros(1,numClass)}$
When i try to run with octave, it gives " linIt undefined" error. I googled "linIt" functions , but i can not reach any information about linIt. Can you give information about this "linIt" function?
Thanks.
The user s-gupta whose repository you're using seems to have another repository called utils, where he defines this function https://github.com/s-gupta/utils/blob/master/matlab/linIt.m
Essentially it seems to be a tiny helper function that converts an array to its linearly indexed column-vector, i.e.
function a = linIt(A)
a = A(:);
end

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.

Passing a structure of parameters to a Level 1 m-code S-function in Simulink

I am trying to pass a structure of parameters to an S-function in MATLAB. I have a bunch of parameters and I would like to avoid passing them like this:
% The general form of an MATLAB S-function syntax is:
% [SYS,X0,STR,TS,SIMSTATECOMPLIANCE] = SFUNC(T,X,U,FLAG,P1,...,Pn)
I would prefer passing a single structure that includes all of my parameters. I loaded the data into the Model Workspace as:
First I tried ( in response to Phil):
function [sys,x0,str,ts,simStateCompliance]=system1(t,x,u,flag,DATA_HMMWV)
sizes.NumInputs = 2;
also,
But I get this error:
Phil, this is why I tried to add another input port to the S-Function, I thought that it had to go in there.
I also tried:
sizes.NumInputs = 1;
and I get this error:
Also, are you sure that the DATA_HMMWV is a parameter? It looks slightly different than a Param in this window:
NEW:::: 1/25/2016
Phil, the issue is not with my derivative function, the issue is that I am still not passing the structure into the function. Here is a picture. Notice the data is in the Model Workspace and I passed it (DATA_HMMWV) to the function, but when I stop the simulation at line 13 (debugging mode), DATA_HMMWV is not in the function workspace.
If the code continues ( to flag = 1) we get:
If the code continues to run, it crashes with this error:
So, there were not enough input arguments passed to the function. Also, the function is very simple:
%
function sys = mdlDerivatives(t,x,u,DATA_HMMWV)
sys = DATA_HMMWV.g;
% end mdlDerivatives
It just tries to grab a parameter from the structure.
Setup 1: Load the data as a structure into the base workspace and run the simulink model
clear;
clc;
close all
PlantName = 'untitled';
open(PlantName)
TFinal = 10;
load DATA_HMMWV.mat
sim(PlantName, TFinal)
Setup 2:
Setup 3:
When you double click on this model, specify the structure that you would like to pass to the S-function as:
Setup 4: your functions should also have the structure in it:
function [sys,x0,str,ts,simStateCompliance]=system1(t,x,u,flag,DATA_HMMWV)
and any other functions that you need the structure in, for example:
case 1
sys = mdlDerivatives(t,x,u,DATA_HMMWV);
then,
function sys = mdlDerivatives(t,x,u,DATA_HMMWV)
Now, you have passed a strucure to level-1 S-function!

Matlab assignment - return only odd elements

I wrote code similar to that posted by FeliceM, but when I tried to include the recommended changes/additions I got the following error:
Warning: File: odd_index.m Line: 5 Column: 18
Function with duplicate name "odd_index" cannot be called.
Here's my code:
function odd_index
M=[1:5; 6:10; 11:15; 16:20; 21:25];
M=M(1:2:end, 1:2:end);
end
function M_out = odd_index(M)
M_out = M(1:2:end, 1:2:end);
end
Not quite sure what I'm doing wrong.
It appears to be simply a problem of naming your two functions the same name. Rename the second function (and make sure it's in a separate file with the same name as itself). Really, the first one isn't a function at all, but appears to be a script.
You may want to refer to the MATLAB Getting Started help documentation for more information on functions and scripts. http://www.mathworks.com/help/matlab/getting-started-with-matlab.html