Not enough input arguments in a function script

I have a main script in which I am using diffrent functions that I made but it is giving an error in one of the function. And the error in one line of a particular function is 'Not enough input arguments'. Line 17 has an error Here is the function:
function [NodeMat]=Function_Sofistik_ReadNodes(FileName,NodeIDstr)
% clear all;close all;clc
% FileName=['MainModel' '_NODES_' '.lst']; % Sofistik .lst file name
% NodeIDstr='DATA EXTRACTION: NODE DATA'; % Header to search in Sofistik nodes output file
addpath Functions;
addpath SofistikFiles ;
% Read node coordinates output file and save data
fid_node=fopen(FileName,'r');
countRN=1;
while 1
line_node=fgetl(fid_node);
if ~ischar(line_node);
break;
end
NodeFileData{countRN,1}=line_node;
if (isempty(NodeFileData{countRN,1},NodeIDstr))~=1
break;
NodeLineNo=countRN;
end
countRN=countRN+1;
end
fclose(fid_node);
% Save Nodes
countSN=1;
while isnumeric(str2num(NodeFileData{NodeLineNo+countSN+1,1})) && isempty(str2num(NodeFileData{NodeLineNo+countSN+1,1}))==0
TempNode=str2num(NodeFileData{NodeLineNo+countSN+1,1});
if length(TempNode)==4 && TempNode(1)~=0
NodeMat(countSN,:)=str2num(NodeFileData{NodeLineNo+countSN+1,:});
end
countSN=countSN+1;
end

 Accepted Answer

Line 17 is the first place that you refer to NodeIDstr inside the function.
You appear to be calling the function with only a single parameter (the file name) without passing in the second parameter (NodeIDStr)
if (isempty(NodeFileData{countRN,1},NodeIDstr))~=1
You are passing two parameters to isempty() but isempty() only accepts one parameter.
You are not seeing the error from isempty() because isempty() does not get control to check number of parameters until after NodeIDstr is evaluated to try to construct the second parameter.
I suspect you are thinking something like
if contains(NodeFileData{countRN,1}, NodeIDstr)
but if so then you need to be careful because contains() is happy to match substrings. contains('golum101','lum10') is true for example. You should probably break out the node name from the line and compare for equality. For example,
lineid = regexp(NodeFileData{countRN,1}, '(?<=^\s*Node\s+=\s+)\w+(?=\s*:)', 'match');
if strcmp(lineid, NodeIDstr)
This particular code would be for a case in which the node input lines were of the form
Node = NAME : something
where 'Node' =' and ':' are literal and NAME is the part you are looking to match and the something will be ignored

12 Comments

@Walter Roberson
Hello I just tried this but nothing happened infact it is giving me another error now as 'Too many input arguments'
Well post your revised code, and attach a mat file that contains the values of the variables that you are passing in to the function, and include a sample of the input file.
I do not seem to have Samples.txt to test with.
Why are you overwriting FileName in your script?
Sorry I forgot to add the sample file. I am overwritting it because everytime sample is changing.
FileName=['SofistikFiles\MainModel' '_NODES_' '.lst']; % Sofistik .lst file name
% Read node coordinates output file and save data
FileName=['SofistikFiles\MainModel' '_NODES_' num2str(SampleMat(ns,1)) '.lst'];
What is the point of doing the assignment on the % Sofistik .lst file name line when you are going to overwrite the variable 3 lines further down?
Sorry I didn't understood what you want to say
addpath Functions;
addpath SofistikFiles ;
Why are you doing those? You are only calling standard MATLAB functions such as regexp and strcmp within the routine, so why add the path to functions?
You do fopen() within the routine, so possibly your second addpath is intended to provide the directory that the files are to be found in. But if that is the case, instead of using addpath, your driver routine should be putting that directory name in as part of the name of the file to open, such as
FileName=['SofistikFiles\MainModel' '_NODES_' num2str(SampleMat(ns,1)) '.lst'];
but you already do that. Is there another layer of SofistikFiles directory involved that needs to be search, like
SofistikFiles\SofistikFiles\MainModel_NODES_1.lst
??
Note: we do not have MainModel_NODES_1.lst or MainModel_NODES_2.lst to test with.
Note also: We recommend that you use fullfile() instead of building filenames with embedded directory seperators
Sofist_dir = 'SofistikFiles';
...
FileName = fullfile(Sofist_dir, [MainModel' '_NODES_' num2str(SampleMat(ns,1)) '.lst']);
I tried this but now it says the output argument NodeMat is not assigned during the call of the function
Your code only assigns into NodeMat under a combination of conditions. As outside observers who do not have copies of your files to look at (hint, hint), we must assume that the combination of circumstances is never true in at least one of the files you are dealing with.
Note: str2num() is unlikely to be the correct function to call: it evaluates whatever it is passed as if it is code, and returns the result. So for example if the file happened to contain 'tf([1 2],[1 0 0])' then str2num() would construct a transfer function from it. Your isnumeric() call would then detect that it wasn't what you wanted. But it would be better to use str2double() which always returns a scalar numeric value that is NaN if the content was not something that represented a scalar numeric value.
ok thank you so much for the help

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!