フィルターのクリア

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.

34 ビュー (過去 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 Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by