for loop with eval and sprintf with underscore in

Sorry about that,
I've been trying really hard to find any solution from other answers, but or I don't understand them or it doesn't work.
I've also red that is bad to dynamically use 'eval' and 'sprintf', however, even though I'm willing to learn, I am not able to do in another way (and I've tried it).
I've many .txt file:
X12345678_1_ABC_ABCD_ABCDE_ABCDEF.txt
% Load .txt files into workspace
files = dir('*.txt');
for i=1:length(files);
eval(['load ' files(i).name ' -ascii']);
end
%% I dynamically try to do something with my variable
for i=1:length(files);
filename = files(i).name(1:end-4); %%%I get out the .txt
temp = eval(sprintf('X%s\_1\_ABC\_ABCD\_ABCDE\_ABCDEF',filename));
%insert the matrix of the text file into a new 3D matrix
threeD_matrix (i,:,f) = temp;
end
Warning: Escape sequence '_' is not valid. See 'help sprintf' for valid escape sequences. Error using eval Undefined function or variable
Thank you so much for your help!!!!!

1 comentario

Stephen23
Stephen23 el 14 de Nov. de 2023
Editada: Stephen23 el 14 de Nov. de 2023
"I've many .txt file: X12345678_1_ABC_ABCD_ABCDE_ABCDEF.txt"
The simple solution is to avoid LOAD for textfiles. Use READMATRIX, READTABLE, etc.
At the time this question was posted, CSVREAD or DLMREAD would likely have worked.

Iniciar sesión para comentar.

Respuestas (2)

Jan
Jan el 7 de Abr. de 2014
Two general advices:
1. Avoid eval in general:
eval(['load ' files(i).name ' -ascii']);
Better:
load(files(i).name, '-ascii');
2. Join folder names by fullfile instead of sprintf with hard coded separators.

1 comentario

Elisa
Elisa el 8 de Abr. de 2014
Thanks Jan! I didn't know, who to do it! I'll follow your tips for next time! Thanks a lot!

Iniciar sesión para comentar.

Star Strider
Star Strider el 7 de Abr. de 2014
Editada: Star Strider el 7 de Abr. de 2014
To put a backslash (\) into a string, you need to use a double backslash (\\):
filename = 'myfile.txt';
R = sprintf('X%s\\_1\\_ABC\\_ABCD\\_ABCDE\\_ABCDEF', filename)
produces:
R =
Xmyfile.txt\_1\_ABC\_ABCD\_ABCDE\_ABCDEF
The ‘eval’ function is not considered efficient programming practice, but it may be the only way to get a variable generated as a string into the workspace as a variable. It is not really ‘wrong’ though.

10 comentarios

Elisa
Elisa el 7 de Abr. de 2014
Dear Star,
Many thanks for your answer.
I need to get the underscore not the backslash, though:
X12345678_1_ABC_ABCD_ABCDE_ABCDEF
Sorry about the confusion!
To do that, just put them in as ordinary characters:
filename = 'myfile.txt';
R = sprintf('X%s_1_ABC_ABCD_ABCDE_ABCDEF', filename)
produces:
R =
Xmyfile.txt_1_ABC_ABCD_ABCDE_ABCDEF
It gives me an error. That is why I am getting crazy with that.
The error says:
Error using eval Undefined function or variable 'X12345678'.
Error in my_script (line 36) temp = eval(sprintf('X%s_1_ABC_ABCD_ABCDE_ABCDEF',filename)); >> Line 36 is the line where we wrote:
temp = eval(sprintf('X%s_1_ABC_ABCD_ABCDE_ABCDEF',filename));
Thanks for your help!!!!!
Star Strider
Star Strider el 7 de Abr. de 2014
Editada: Star Strider el 7 de Abr. de 2014
I believe I’m beginning to understand what you’re doing. To find out what the variables are in your ‘.mat’ files, see the documentation for that version of whos and its friends. (NOTE that there is also a version of whos that operates in a different context but provides the same sort of information. This is the first time I realised there are actually two versions of that function, so thanks!)
I’m not certain what you want to do after you load your files. If this isn’t the direction you want to go with them or the variables in them (since I have no idea how they’re organised and what they contain), please describe in some detail what you want to do.
Elisa
Elisa el 7 de Abr. de 2014
Editada: Elisa el 7 de Abr. de 2014
Thank you so much Star Strider for your help!!!
I have 3 folders. Each of them contains many subfolders, which contains many .txt files. I have written a loop, so, I could open the folder, load the .txt files for each folder and operates with them; then cd back to the folder, open the next one, load the .txt file and operates with them; cd back etc. without having to write their name all the time. therefore I used 'eval' and 'sprintf'.
It 's work for one folder, where the .txt files didn't have such a long name. Now, with this one it doesn't work :(
Error using eval
Undefined function or variable 'X12345678'.
Error in my_script (line 36) temp = eval(sprintf('X%s_1_ABC_ABCD_ABCDE_ABCDEF',filename));
in fact, the variable is not called 'X12345678' but 'X12345678_1_ABC_ABCD_ABCDE_ABCDEF'
I don't know!!!!
My pleasure!
I’m nevertheless still a bit lost.
What happens if you just write:
disp(X12345678_1_ABC_ABCD_ABCDE_ABCDEF)
Does it display the contents of the variable? If so, the variable is already in your workspace.
Since I get the impression that this may actually be a file name, see what this does:
which X12345678_1_ABC_ABCD_ABCDE_ABCDEF -all
If it’s a file in MATLAB’s path, that should find it. You may have to change that to:
which X12345678_1_ABC_ABCD_ABCDE_ABCDEF.txt -all
or whatever its extension is. The function version of that command will return the entire file path:
datfile = which('X12345678_1_ABC_ABCD_ABCDE_ABCDEF.txt')
or
datfile = which('X12345678_1_ABC_ABCD_ABCDE_ABCDEF.txt','-all')
See the documentation for which for more details.
Elisa
Elisa el 7 de Abr. de 2014
Movida: Voss el 14 de Nov. de 2023
Dear Star Stride,
Thanks a lot again!
I used
disp(....) and the variable is there
which X12345678_1_ABC_ABCD_ABCDE_ABCDEF -all
says that:
X12345678_1_ABC_ABCD_ABCDE_ABCDEF is a variable
which X12345678_1_ABC_ABCD_ABCDE_ABCDEF.txt -all
says that:
X12345678_1_ABC_ABCD_ABCDE_ABCDEF.txt not found
datfile = which('X12345678_1_ABC_ABCD_ABCDE_ABCDEF.txt','-all')
gives: datfile = {}
Elisa
Elisa el 7 de Abr. de 2014
Movida: Voss el 14 de Nov. de 2023
thanks for your help!!!! I'll manually change the annoying name. X12345678 without _1_ABC_ABCD_ABCDE_ABCDEF.txt works fine strangely.
Star Strider
Star Strider el 7 de Abr. de 2014
Movida: Voss el 14 de Nov. de 2023
My pleasure!
Sorry for the delay — sleeping here (GMT-6).
My pleasure!
Sorry for the delay — sleeping here (GMT-6).

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 7 de Abr. de 2014

Editada:

el 14 de Nov. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by