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
채택된 답변
Walter Roberson
2020년 6월 24일
편집: Walter Roberson
2020년 6월 24일
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
Thank You so much
@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
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Search Path에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
