Filter löschen
Filter löschen

String starting with ' and ending with '

3 Ansichten (letzte 30 Tage)
Francois Piche
Francois Piche am 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

Antworten (2)

Thomas
Thomas am 3 Jul. 2012
Bearbeitet: Thomas am 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 Kommentare
Francois Piche
Francois Piche am 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 am 3 Jul. 2012
Bearbeitet: Thomas am 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...

Melden Sie sich an, um zu kommentieren.


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

Kategorien

Mehr zu Workspace Variables and MAT-Files finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by