Azzera filtri
Azzera filtri

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.

30 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 1 Mag 2024
Modificato: Sulaymon Eshkabilov il 1 Mag 2024
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 Commenti
Sulaymon Eshkabilov
Sulaymon Eshkabilov il 1 Mag 2024
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 il 1 Mag 2024
Modificato: Stephen23 il 1 Mag 2024
"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.

Accedi per commentare.

Più risposte (1)

Stephen23
Stephen23 il 1 Mag 2024
Modificato: Stephen23 il 1 Mag 2024
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 Commenti
Stephen23
Stephen23 il 1 Mag 2024
Modificato: Stephen23 il 1 Mag 2024
"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 il 1 Mag 2024
The only thing I have changed is the directory names. Everything else is verbatim. Anyway, it works now thanks to Sulaymon. Thank you.

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by