MATLAB : passing cell arguments - matlab

I have a function :
function myFunc(myStruct)
% file can contains one or a list of files
file = {fullfile(pwd,myStruct.name)}
end
from another file , when I call myFunc
myStruct.name = {'toto','titi','tata'}
myFunc(myStruct);
I got an error ,function isn't definied for cell
and I would like to pass to the field myStruct.name a string or a list of strings :
I mean that myStruct.name can accept one argument 'toto' or list of arguments {'toto','titi'}
When
How could I do that ?
Thanks

Finally we have a complete question.
Read the exception carefully, how you asked your question tells me that you did not understand it. The exception says that fullfile is not defined for cell inputs, which is simply fact. You have to call fullfile for each folder that should be generated, thus a for-loop is required, with something like file{idx} = fullfile(pwd,myStruct.name{idx}) in it.

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.

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 Array of structures: string not working

I am reading an input from a file and importing it into my data to run in Matlab:
parts = strread(tline,'%s','delimiter',';')
employee(i).name = parts(1);
employee(i).salary= str2double(parts(2));
Then I try to print it out:
for i = 1:3
fprintf('salary: %.2f\n',employee(i).salary);
fprintf('employee name: %s\n',employee(i).name);
end
The salary prints with no problem. But the for the variable "name" it gives an error:
Error using fprintf
Function is not defined for 'cell' inputs.
fprintf('employee name: %s\n',employee(i).name);
I looked for some other examples:
access struct data (matlab)
How do I access structure fields dynamically?
Matlab Error: Function is not defined for 'cell' inputs
How do i define a structure in Matlab
But there is nothing to address this case, where only string is not working.
I have not explicitly declared the data as struct, i.e. inside the code there is nowhere the "struct" word is included, but Matlab apparently automatically understand it as an "Array of structures".
Any hints what might be missing here?
All comments are highly appreciated!
The issue is that employee(k).name is a cell (check with iscell(employee(1).name)) and the format string %s doesn't know how to handle that.
The reason that it is a cell is because strread returns a cell array. To grab an element from the result (parts), you want to use {} indexing which returns a string rather than () which will return a cell.
employee(i).name = parts{1};

Getting two parameters from c function in matlab

i had a code that c send back 1 number (mex)
the matlab code was
vMsg=unit32(Gateway_test_app(2))
now i added 1 more return value to Gateway_test_app(2) which is s STRING
what i need to do to get the two values back
i was thinking about something like this:
[vMsg,errMsg]=??????(Gateway_test_app(2))
what should i put in the ????? place?
thx for any help
johnny.
ps
using codegen and need not to get err when building
First call the function and store the two outputs, then run your extra function unit32 (what does it do, by the way?) on the first output only:
[vMsgOriginal, errMsg] = Gateway_test_app(2);
vMsg = unit32(vMsgOriginal);
This assumes that you don't want to process your new string output through your unit32 function.

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