How i use a variable name to do a fid=fopen?

Hi,
I execute a program in this way program('Untitled')
and i wan't that
if exist ('file','var')
input_file = ('Untitled.inp');
fid = fopen(input_file);
where the name of file it's what i write, if i call "program('test')" the is input_file = ('test.inp');
I tried this:
if exist ('file','var')
input_file = ('file.inp');
fid = fopen(input_file);
but doesn't work.
Any help?

 채택된 답변

Walter Roberson
Walter Roberson 2014년 2월 2일

1 개 추천

input_file = [file '.inp'];

추가 답변 (1개)

Jos (10584)
Jos (10584) 2014년 2월 2일

0 개 추천

Create a function m-file, where you can use a variable.
function MyProgram (MyInputFile)
% MYPROGRAM (FILE) opens the file with the name FILE, if it exists.
% Use it like this: >> MyProgram('test.dat')
if exist (MyInputFile, 'file')
fid = fopen(MyInputFile,'rt')
% do something useful here
fclose(fid) ;
else
disp(['The file ' MyInputfile ' does not exist.']) ;
end

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

질문:

2014년 2월 2일

댓글:

2014년 2월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by