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 件のコメント
Anisha Menro
2020 年 6 月 30 日
Thank You so much
Anisha Menro
2020 年 6 月 30 日
@Walter Roberson
Hello I just tried this but nothing happened infact it is giving me another error now as 'Too many input arguments'
Walter Roberson
2020 年 7 月 1 日
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.
Walter Roberson
2020 年 7 月 2 日
I do not seem to have Samples.txt to test with.
Walter Roberson
2020 年 7 月 2 日
Why are you overwriting FileName in your script?
Anisha Menro
2020 年 7 月 2 日
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?
Anisha Menro
2020 年 7 月 2 日
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']);
Anisha Menro
2020 年 7 月 2 日
I tried this but now it says the output argument NodeMat is not assigned during the call of the function
Walter Roberson
2020 年 7 月 2 日
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.
Anisha Menro
2020 年 7 月 2 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
