String starting with ' and ending with '

4 views (last 30 days)
Francois Piche
Francois Piche on 3 Jul 2012
I have a function that calls a filename. The filename must be typed in with the string delimiter ' ' included into the argument. I have string variables holding the filenames. How do I call this function using the string variables and have the string delimiter explicitly passed to the function. If I just give it the variable name, it chokes because the delimiters are not there. Here is the structure of how the function opens the file.
function [A, B]=testfunction(file);
% [data, header]=testfunction(file)
% This function reads in a file and its header information
fid=fopen(file,'r','ieee-be');
. . .
Thanks,
Francois

Answers (2)

Thomas
Thomas on 3 Jul 2012
Edited: Thomas on 3 Jul 2012
Here is my function file
function [A,B]=readfunction(file)
file_name=file % donot need this just to show it goes to variable
fid=fopen(file_name);
end
Now if I give the following command
>> readfunction('hello.m')
file_name =
hello.m
It comes in without the ' ' delimiters.. and opens the file just fine..
  2 Comments
Francois Piche
Francois Piche on 3 Jul 2012
The argument you are calling in your example function has the filename in ' ' string delimiter. If you tried calling your test function with a string variable it would not work.
Try readfunction(file_name) and see what happens. I am trying to use the function to open multiple files with different names which are stored in string variables.
Thomas
Thomas on 3 Jul 2012
Edited: Thomas on 3 Jul 2012
you need to use the ' ' delimiter to call a file, you cannot do without.. and even if you use the ' ' delimiter in the calling the function the variable will not have the ' ' delimiter..
>> name='hello.m';
>> readfunction(name)
file_name =
hello.m
how ever if you want to use it again you need to add the single quotes to filename as
new=sprintf('''%s''',file_name)
readfunction(new)
Works as it should...

Sign in to comment.


Walter Roberson
Walter Roberson on 3 Jul 2012
filenames = {'file1.txt', 'file2.txt', 'file3.txt'};
for K = 1 : length(filenames)
thisfile = filenames{K};
testfunction(thisfile)
end

Products

Community Treasure Hunt

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

Start Hunting!