i want to save and name a matrix each loop iteration

4 views (last 30 days)
i have a banch of txt file , i am writing a function that read data from those txt files convert them into a matrix than i want to save this matrix with the same txt files at each iteration .
it will be somthing like :
dirName = 'C:\Users\benz\datas'; %# folder path
files = dir( fullfile(dirName,'*.txt') ); %# list all *.xyz files
files = {files.name}
for i=1:length(files)
filein=files{i};
fileid = fopen(filein) ;
data = textscan(fileid ,'%f %f %f %*f %*f %*f %*f','HeaderLines',12,'Delimiter','');
%%%%%%%%%% ...use data in many operations and functions to generate a Matrix called "Matrix"...... %%%%%%%%%%%%%.........
..........................................................................................................................................................................................
%%%%%%% i want to save Matrix with same txt file name in each iteration loop !!!!!!!!!!!!!!!!
end
i want to save Matrix with same txt file name in each iteration loop !!! what is hte instruction ?*
thanks

Answers (1)

Walter Roberson
Walter Roberson on 30 Aug 2021
I am tired of arguing with people who want to create variables dynamically. Creating variable names dynamically is a Bad Idea, like running an electric saw without a hand-guard -- but when we tell people that, they almost alway argue that their case justifies it.
So here it is. I have adapted your code to create the variable names dynamically. Use it at your own risk.
In summary, what the code is doing is that for every assignment, it is creating a new temporary script that assigns the data to the variable name, and then it executes the script, and then deletes the script. It stores these scripts in a temporary folder inside the official location for temporary files on your system, and it makes sure that the temporary folder will be deleted afterwards.
You might be wondering what all those ZZZZZZ_ prefixes are. I put those in to greatly reduce the likelyhood that the variable names I use will accidentally clash with existing variable names or with the name of any of the .txt files. Remember, any file name is going to become a variable name, so if you happen to have a file named files.txt then the code is going to clobber your files variable while it is in the middle of being used in the loop. That's the risk you have to take when you create variable names according to files: the file name might happen to match a variable name that you are already using, and then you are out of luck.
ZZZZZZ_TD = tempname();
try
mkdir(ZZZZZZ_TD);
cleanMe = onCleanup(@() rmdir(ZZZZZZ_TD, 's'));
catch ME
error('Could not create temporary directory "%s"', ZZZZZZ_TD);
end
dirName = 'C:\Users\benz\datas'; %# folder path
files = dir( fullfile(dirName,'*.txt') ); %# list all *.xyz files
files = {files.name}
for i=1:length(files)
filein=files{i};
fileid = fopen(filein) ;
data = textscan(fileid ,'%f %f %f %*f %*f %*f %*f','HeaderLines',12,'Delimiter','');
%%%%%%%%%% ...use data in many operations and functions to generate a Matrix called "Matrix"...... %%%%%%%%%%%%%.........
..........................................................................................................................................................................................
%%%%%%% i want to save Matrix with same txt file name in each iteration loop !!!!!!!!!!!!!!!!
[~, ZZZZZZ_basename, ~] = fileparts(filein);
ZZZZZZ_data_array = cell2mat(data); %textscan returns cell
ZZZZZZ_temp_script_name = "assign_" + ZZZZZZ_basename;
ZZZZZZ_TF = fullfile(ZZZZZZ_TD, ZZZZZZ_temp_script_name + ".m");
[ZZZZZZ_tfid, ZZZZZZ_msg] = fopen(TF, 'w');
if ZZZZZZ_tfid < 0
error('Failed trying to open temporary file "%s"', ZZZZZZ_TF);
end
fprint(ZZZZZZ_tfid, '%s = data_array;\n', ZZZZZZ_basename);
fclose(ZZZZZZ_tfid);
clear(ZZZZZZ_temp_script_name); %signals to MATAB to refresh the file
run(ZZZZZZ_TF);
delete(ZZZZZZ_TF);
end
clear cleanMe %delete directory
  3 Comments
Walter Roberson
Walter Roberson on 4 Sep 2021
So after
fprint(ZZZZZZ_tfid, '%s = data_array;\n', ZZZZZZ_basename);
you could add
fprint(ZZZZZZ_tfid, "save('%s.mat', '%s')", ZZZZZZ_basename, ZZZZZZ_basename);
Walter Roberson
Walter Roberson on 4 Sep 2021
Or, you know, you could Just Not Do That. What you want to do does NOT require saving a variable with the name of the text file.
dirName = 'C:\Users\benz\datas'; %# folder path
files = dir( fullfile(dirName,'*.txt') ); %# list all *.xyz files
files = {files.name}
for i=1:length(files)
filein=files{i};
fileid = fopen(filein) ;
data = textscan(fileid ,'%f %f %f %*f %*f %*f %*f','HeaderLines',12,'Delimiter','');
%%%%%%%%%% ...use data in many operations and functions to generate a Matrix called "Matrix"...... %%%%%%%%%%%%%.........
..........................................................................................................................................................................................
%%%%%%% i want to save Matrix with same txt file name in each iteration loop !!!!!!!!!!!!!!!!
[~, ZZZZZZ_basename, ~] = fileparts(filein);
clear ZZZZZZ_temp
ZZZZZZ_temp.(ZZZZZZ_basename) = cell2mat(data);
save(ZZZZZZ_basename + ".mat", "-struct", "ZZZZZZ_temp");
end
No variable dynamically created !!!
When you save() with the -struct flag, then MATAB takes the structure named, and takes every field of it and saves the fields as individual variables in the .mat . The ZZZZZZ_temp.(ZZZZZZ_basename) use dynamic field naming to construct a field in the (fixed name!) structure variable ZZZZZZ_temp so afterwards it is a struct with a single field name that is derived from the file name. And then the save -struct will trigger MATLAB to use that field name (so the file name) as the name of the variable in the .mat file.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!