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.
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Srh Fwl
il 1 Mag 2024
Commentato: Srh Fwl
il 1 Mag 2024
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.
0 Commenti
Risposta accettata
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
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
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.
Più risposte (1)
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')
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')
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
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.
This is also a good example of https://en.wikipedia.org/wiki/Shotgun_debugging
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.
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!