MATLAB Answers

I have a variable with a string that I would like to use as a filename, how do I input that string into a function rather than the variable name?

조회 수: 1(최근 30일)
AThomas 2020년 6월 21일
댓글: AThomas 2020년 6월 22일
As below, I have a dialog box asking me for a filename.
It will then add the file extension onto the end.
answer = inputdlg(fileName,dlgtitle,dims,definput);
fileName1 = strcat(answer,'.dxf');
FID = dxf_open(fileName1);
I've tried numerous different ways, and they all end up with errors.
The code works normally if I hardcode the file name into the dxf_open function. e.g. 'Export.dxf'

채택된 답변

the cyclist
the cyclist 2020년 6월 22일
편집: the cyclist 2020년 6월 22일
I think I see the problem. Did you notice that the output of the inputdlg command is a cell array? You need the contents of that cell array, which is the character array you want. So you need something like this:
fileName1 = strcat(answer{:},'.dxf');
Note the {:} after answer, to get at the contents of the cell array.
  댓글 수: 1
AThomas 2020년 6월 22일
Thank you very much for your help.
The code works with the {:} in the dxf_open portion
fileName1 = strcat(answer,'.dxf');
FID = dxf_open(fileName1{:});

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

추가 답변(1개)

the cyclist
the cyclist 2020년 6월 21일
I don't have dxf_open, but the following -- which is effectively the same as what you posted -- works for me:
answer = 'test';
fileName1 = strcat(answer,'.dxf');
fid = fopen(fileName1,'w');
  댓글 수: 1
AThomas 2020년 6월 22일
Thanks, but when I try that I get the error "Error using dxf_open Too many input arguments"

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


Community Treasure Hunt

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

Start Hunting!

Translated by