MATLAB: How to call data from files named with dynamic index

data importdynamic namesindexingMATLABplot data

I know that a similar question has been turned down many times as we don't EVER want to use dynamic vaiable names in programming. The fact is that I'm trying to make my life easier when plotting measurement results. The files are named after the sample as in sample001.csv or SampleName_<Date>.txt (and I cannot change the export or naming format). All I want to do is:
% for each of these text files, plot a set column as x and another as y
figure
hold on
% k is a high number and the number of files to import for plotting
for n = 1:k
plot(sample<n>(:,14),sample<n>(:,17))
end
I can think of two ways to do this:
  1. Break the Rules of Matlab and somehow implement the dynamic variable name anyway (since the files per default imprt with the file name as variable name). I've tried some things with num2str or putting a char or string variable into plot but couldn't get it to work
  2. Get the files to import to a multidimensional (in my case 3D) array or table from which I can call the data in a conventional method
I presume the second will be the experts' choice, therefore, how can I
  • select a great number of files for importing (best from a Windows Explorer "open a file" dialog)
  • forget the header in the files (it would be nice to keep the column names but it makes accessing the rest of the data so much more complicated so I'd rather scrap the header and import the actual data as numeric matrix)
  • make sure the decimal separator is correct
  • import them in a way that I can access the data of "sample345.txt" as array(:,:,345) or something like that
  • get the whole thing to be robust so that it works if the data has different (arbitrary) height (and possibly width) in each file, contains strings as well as numbers or files are missing
Any help much appreciated.
______
PS: I've come up with something similar already but the problem is that it only works for numeric data with no header etc. and overwrites the variable every time. Sorry for the syntax, I'm not a programmer
for k=(1:3:72)
textFileName = ['sample_' num2str(k) '.txt'];
textData = readmatrix(textFileName);
sz = size(textData);
x = sz(1);
dehnung = textData(:,4);
spannung = textData(:,5);
displacement = textData(:,10);
length = textData(1,17);
z=displacement(1);
for i = 1:x
displacement(i) = displacement(i) - z;
dehnung(i) = displacement(i)/(length(1)*1000)*100;
end
plot(dehnung,spannung)
hold on
end

Best Answer

"select a great number of files for importing (best from a Windows Explorer "open a file" dialog)"
If you really have a "great number" of files your user is going to dislike this way of selecting them. Most likely simply selecting the folder and using dir with a filename filter would be simpler and less fiddly to use. But each to their own...
[C,P] = uigetfile(..,'MultiSelect','on');
N = numel(C);
D = cell(1,N);
for k = 1:N
F = fullfile(P,C{k});
M = .. whatever file importing function you want to use
.. whatever processing you want to do
D{k} = M;
end
You can access the filenames and filedata using basic indexing, e.g. for the third file:
C{3} % filename
D{3} % imported data
A simple alternative is to import the data into the same non-scalar structure as dir returns, e.g.:
P = 'absolute/relative path to the folder where the files are saved';
S = dir(fullfile(P,'sample_*.txt'));
for k = 1:Nnumel(S)
F = fullfile(P,S(k).name);
M = .. whatever file importing function you want to use
.. whatever processing you want to do
S(k).data = M;
end