필터 지우기
필터 지우기

How can I access the value of a string variable? I need to construct a file path from a string variable, but keep getting the variable name and not the string.

조회 수: 12 (최근 30일)
I want to construct a file path, "f" that I will use many times in a Matlab m file. From advice in an answer to someone else's question, I am using the "fullfile" function. However, I am having what seems like a silly problem. Please see the example below.
I want to put "directory1" in the file path to get:
/Users/me/ch4/directory1/directory2/results/file_1.dat
However, when I do:
directory_name = "directory1";
f = fullfile('/Users/me/ch4', directory_name, 'directory2/results/file_1.dat')
I get:
Error using matlab.io.ImportOptions/readtable (line 503)
Unable to find or open '/Users/me/ch4/directory_name/directory2/results/file_1.dat'. Check the path and filename or file permissions
How can I substitute the contents of the variable and not the variable name in the file path? Thank you for any advice.

채택된 답변

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2024년 5월 1일
편집: Sulaymon Eshkabilov 2024년 5월 1일
It can be done these ways.
Way1:
directory_name = "directory1";
f = fullfile(['/Users/me/ch4/', directory_name, '/directory2/results/file_1.dat']);
Or
Way 2:
directory_name = "directory1";
f = strcat(['/Users/me/ch4/', directory_name, '/directory2/results/file_1.dat']);
  댓글 수: 4
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2024년 5월 1일
If you want to get the file directory, then use one of these:
Note: " " vs. ' '
directory_name = 'directory1';
f = fullfile(['/Users/me/ch4/', directory_name, '/directory2/results/file_1.dat']);
Or
directory_name = 'directory1';
f = strcat(['/Users/me/ch4/', directory_name, '/directory2/results/file_1.dat']);
Stephen23
Stephen23 2024년 5월 1일
편집: Stephen23 2024년 5월 1일
"I figured it out. I put the square brackets only around "directory_name.""
No, you do not need superfluous square brackets only around DIRECTORY_NAME.
"If you want to modify your answer, I would be happy to accept it."
Ugh. Note that Sulaymon Eshkabilov's code concatenates everything together using square brackets and then calls FULLFILE and STRCAT... which then do absolutely nothing. Do not follow their advice of filling your code with unused functions that do nothing, without understanding what the code does.
Do NOT use this code.

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

추가 답변 (1개)

Stephen23
Stephen23 2024년 5월 1일
편집: Stephen23 2024년 5월 1일
It works perfectly, exactly as you showed in your question:
directory_name = "directory1";
f = fullfile('/Users/me/ch4', directory_name, 'directory2/results/file_1.dat')
f = "/Users/me/ch4/directory1/directory2/results/file_1.dat"
The problem is that the code you actually used is something like this:
% v v ooops
f = fullfile('/Users/me/ch4', 'directory_name', 'directory2/results/file_1.dat')
f = '/Users/me/ch4/directory_name/directory2/results/file_1.dat'
Do not use square brackets anywhere. If you do, you are making a big mistake (just like Sulaymon Eshkabilov did). Your original approach using FULLFILE is the best approach, the problem is not solved by square brackets.
  댓글 수: 3
Stephen23
Stephen23 2024년 5월 1일
편집: Stephen23 2024년 5월 1일
"Sorry, I still don't understand what the problem is with the square-brackets solution."
It is not a solution to your problem. Square brackets are a concatenation operator: when you place square brackets around one array like this:
f = strcat('/Users/me/ch4/', [directory_name], '/directory2/results/file_1.dat');
% ^ ^ these do nothing
then you are concatenting that one array with... nothing else. This is the definition of pointless.
It is clear that the actual problem lies (or lay) somewhere else. We could help you to debug this properly if you showed us the actual code you used (not edited, simplified, modified, changed, etc for this forum). Verbatim code please.
Srh Fwl
Srh Fwl 2024년 5월 1일
The only thing I have changed is the directory names. Everything else is verbatim. Anyway, it works now thanks to Sulaymon. Thank you.

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

카테고리

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

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by