Not enough input arguments

6 views (last 30 days)
Daniel Wang
Daniel Wang on 20 Jan 2022
Commented: Image Analyst on 20 Jan 2022
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 Comments
Torsten
Torsten on 20 Jan 2022
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 on 20 Jan 2022
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

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 20 Jan 2022
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 Comments
Daniel Wang
Daniel Wang on 20 Jan 2022
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 on 20 Jan 2022
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.

Sign in to comment.

Categories

Find more on Files and Folders in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!