Why do i get an error in this line function [outBits, outBytes] = ReadBinFile ('check.txt.lz')

The error is
??? function [outBits, outBytes] = ReadBinFile ('check.txt.lz')
|
Error: Function definitions are not permitted at the prompt or in scripts.
please help mee.

Answers (2)

For exactly the reason given--"function definitions are not permitted in scripts or at the command line". Not much else to say except "don't do that!!!"; if you are trying to create the function, open the editor with file
edit ReadBinFile
and place the necessary code in there and save...
doc function
Use the "Getting Started" section in the doc to read the section on programming with scripts and functions to get the general idea.
If you're trying to call an existing function of this name, then you don't use the function keyword; that's only for m-files defining functions.

2 Comments

function [outBits, outBytes] = ReadBinFile ('check.txt.lz')
% [outBits outBytes] = ReadBinFile(FileName)
%
% Read data from file. The first byte is interpreted as integer and
% determines the number of zero-padded bits at the end.
%
% FileName: Name of file
% outBits: bit vector containing the bits of the file
% outBytes: bit vector containing the bytes of the file
fid = fopen('check.txt.lz');
fileBytes = fread(fid)';
% First byte contains padding information
paddBits = fileBytes(1);
outBytes = fileBytes(2:end);
bits_tmp = dec2binvec(outBytes')';
outBits = reshape(bits_tmp(1:end-paddBits),1,numel(bits_tmp)-paddBits);
fclose(fid);
This is the code that i am running but it gives the error on the first line.
When does the error occur? Did you understand the error message? It seems, like the posted code is written to a script file (an M-file, which does not start with the term "function") or created in the command window. Both is not allowed in Matlab.

Sign in to comment.

Your code should be
function [outBits, outBytes] = ReadBinFile (FileName)
% [outBits outBytes] = ReadBinFile(FileName)
%
% Read data from file. The first byte is interpreted as integer and
% determines the number of zero-padded bits at the end.
%
% FileName: Name of file
% outBits: bit vector containing the bits of the file
% outBytes: bit vector containing the bytes of the file
fid = fopen(FileName);
fileBytes = fread(fid)';
% First byte contains padding information
paddBits = fileBytes(1);
outBytes = fileBytes(2:end);
bits_tmp = dec2binvec(outBytes')';
outBits = reshape(bits_tmp(1:end-paddBits),1,numel(bits_tmp)-paddBits);
fclose(fid);
and you need to store it in the file ReadBinFile.m . Then you need to call upon it with the line
[outBits, outBytes] = ReadBinFile('check.txt.lz');

8 Comments

I have stored it in ReadBinFile.m but it gives me error on this line function [outBits, outBytes] = ReadBinFile ('check.txt.lz')
Apparently because you're also trying to execute that line from the command prompt or you also have it in a script file. As I and Walter have said, "Don't do that!!!"
We have to see what you actually executed and the error in context to tell precisely; we can't see your terminal from here.
It wouldn't hurt to attach the m-file, either, altho the error in context will likely be sufficient.
You need to look more closely at what I posted. Notice that the "function" line of what I posted does not end in ('check.txt.lz') and instead ends in (FileName)
You need to take the code in the version I posted and you need to store it in a file named ReadBinFile.m
Then you need to go over to your more recent Question to see how to fix the problem with using dec2binvec()
i have copied ur code on ReadBinFile.m and following is the code for my dec2binvec.m
function [outBits, outBytes] = ReadBinFile (FileName)
% [outBits outBytes] = ReadBinFile(FileName)
%
% Read data from file. The first byte is interpreted as integer and
% determines the number of zero-padded bits at the end.
%
% FileName: Name of file
% outBits: bit vector containing the bits of the file
% outBytes: bit vector containing the bytes of the file
fid = fopen(FileName);
fileBytes = fread(fid)';
% First byte contains padding information
paddBits = fileBytes(1);
outBytes = fileBytes(2:end);
bits_tmp = dec2binvec(outBytes')';
outBits = reshape(bits_tmp(1:end-paddBits),1,numel(bits_tmp)-paddBits);
fclose(fid);
but i get this error in it
Error in ==> dec2binvec at 45
out = logical(STR2NUM([fliplr(out);blanks(length(out))]')');
Error in ==> ReadBinFile at 15
bits_tmp = dec2binvec(outBytes')';
out = logical(STR2NUM([fliplr(out);blanks(length(out))]')');
Matlab is case-sensitive; STR2NUM is not the function name; it's str2num. TMW use uppercase in the doc, but that's simply the way they identify the function as being a function, it's not to be read as literal.
i have changed it to str2num but it still gives error on this line.
Again, it doesn't help w/o all the error message and in context showing what you actually executed plus we see the function doing the calling but not the function itself. And, while you posted the section of the error giving the location you truncated the actual error itself in the first posting and then didn't give any information at all this time....
Repeating myself,
"Then you need to go over to your more recent Question to see how to fix the problem with using dec2binvec()"

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Asked:

on 15 Nov 2015

Commented:

on 17 Nov 2015

Community Treasure Hunt

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

Start Hunting!