String starting with ' and ending with '

조회 수: 6 (최근 30일)
Francois Piche
Francois Piche 2012년 7월 3일
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

답변 (2개)

Thomas
Thomas 2012년 7월 3일
편집: Thomas 2012년 7월 3일
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
Francois Piche
Francois Piche 2012년 7월 3일
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 2012년 7월 3일
편집: Thomas 2012년 7월 3일
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...

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


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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by