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
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
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