Not enough input arguments

조회 수: 2 (최근 30일)
Daniel Wang
Daniel Wang 2022년 1월 20일
댓글: Image Analyst 2022년 1월 20일
function [out]=loadrpc(filename, mnchs, flag)
% mnchs: array, the selected channels to be read.
% if empty, read all data
% flag: the same as Matlab fopen's MACHINEFORMAT
% 'l': ieee-little-endian, such as NT
% 'b': ieee-big-endian, such as UNIX
% default is 'l'
%
% example:
% y=loadrpc('test.rsp'); %read all data
% z=loadrpc('test.rsp', [2, 10 20]); %read channel: 2, 10, and 20
%
if (nargin==1)
mnchs='';
flag='l';
mnchs_n=0;
elseif ((nargin==2) & ~isempty(mnchs))
flag='l';
mnchs_n=max(size(mnchs));
elseif ((nargin==3) & ~isempty(mnchs))
mnchs_n=max(size(mnchs));
else
mnchs='';
mnchs_n=0;
end
fid=fopen(filename, 'r', flag);
if (fid == -1)
out=0;
return;
end
Repurposing some old code written 20 years ago. This code represents the first part of a function used to read the values of .rsp files (binary format; time history data). 'Not enough input arguments' error ocurs at the line: fid=fopen(filename, 'r', flag);
  댓글 수: 2
Torsten
Torsten 2022년 1월 20일
You can not simply run this function "loadrpc".
You must call it with appropriate input arguments for filename, mnchs and flag.
Daniel Wang
Daniel Wang 2022년 1월 20일
I thought that the code I shared defines the function. I would call the function name AFTER the function is defined with the appropriate inputs, right? In which case, the error still comes up during the definition stage.
To get past this error I had to define the input arguments before the function. Is this common practice? I'm very new to functions in MATLAB

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2022년 1월 20일
Wow - bad, fragile code. Not robust at all. For example it does not check filename to see if it's empty. And does check with isfile(filename) to see if it exists before trying to open it.
Anyway, what did you pass in for filename, mnchs, and flag when you called the function?
You didn't just press the green run triangle without assigning values to those variables did you? Because it obviously needs a filename to call fopen(). It can't be just null.
  댓글 수: 2
Daniel Wang
Daniel Wang 2022년 1월 20일
I thought that the code I shared defines the function. I would call the function name AFTER the function is defined with the appropriate inputs, right? In which case, the error still comes up during the definition stage.
To get past this error I had to define the input arguments before the function. Is this common practice? I'm very new to functions in MATLAB
Image Analyst
Image Analyst 2022년 1월 20일
Yes, of course. That defines the function but not the variables. So, in your main program when you get to this line:
[out]=loadrpc(filename, mnchs, flag)
if you haven't defined filename, mnchs, flag, what is it supposed to use for those values? Just make up something? What if you wanted 'test.dat', 5, and 'b' and you didn't pass those in? Is it okay if the program makes up and uses "bogus.dat", 9, and 'l" instead? That might not do what you want. So you have to tell it what you want.
So, like
[out]=loadrpc('mydata.dat', 4, 'b');
or whatever you want/need.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by