Why do i get an error in this line function [outBits, outBytes] = ReadBinFile ('check.txt.lz')
Show older comments
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)
dpb
on 15 Nov 2015
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
Maida Abbasi
on 15 Nov 2015
Edited: Walter Roberson
on 15 Nov 2015
Jan
on 15 Nov 2015
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.
Walter Roberson
on 15 Nov 2015
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
Maida Abbasi
on 16 Nov 2015
dpb
on 16 Nov 2015
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.
Walter Roberson
on 16 Nov 2015
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()
Maida Abbasi
on 17 Nov 2015
Edited: dpb
on 17 Nov 2015
dpb
on 17 Nov 2015
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.
Maida Abbasi
on 17 Nov 2015
Edited: Maida Abbasi
on 17 Nov 2015
dpb
on 17 Nov 2015
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....
Walter Roberson
on 17 Nov 2015
Repeating myself,
"Then you need to go over to your more recent Question to see how to fix the problem with using dec2binvec()"
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!