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
figurehold 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:
- 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
- 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 onend
Best Answer