Importing and renaming multiple files

Later later edit - in case someone needs to o something similar: this is the final code I used to put all files and their columns in a data structure, with as little dynamic naming as I could muster. Thank you all for the help!
%%Allocate imported array to column variable names
% where i is the number of columns in the data file
% and n represents an index (shortcut for channel name)
n = {'_P','_R1','_R2','_T'};
for k=1:length(filename)
for i=1:4
data(k,i).name = strcat(filename{k},n{i});
data(k,i).amplitude = dataArray{:, i};
end
end
%the result is a matrix with k rows and 4 colums.
Later edit: Attached a sample file (saved as .txt as the webpage disagrees with .ASC).
Hello,
I am trying to import multiple data files in matlab for further processing. Each file contains 4 columns, labeld P, R1, R2 and T. I would like to create a variable name for the corresponding column vectors that looks like filename_columnname for easy identification further.
Below I post the code, but the variable names will be the same for all imported files. I read about the contraindications in using evalf so any suggestion would be appreciated (for the section starting with "Allocate imported array to column variable names").
Note: I modified the generated code from the import file menu to go through multiple files chosen by the user.
startRow = 1;
endRow = inf;
formatSpec = '%14f%14f%14f%f%[^\n\r]';
[filename,pathname] = uigetfile('*.ASC','Select files to import','MultiSelect','on');
nfile = length(filename);
for k=1:nfile
fileID{k} = fopen(fullfile(pathname,filename{k}),'r');
dataArray = textscan(fileID{k}, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string', 'HeaderLines', startRow(1)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
for block=2:length(startRow)
frewind(fileID{k});
dataArrayBlock = textscan(fileID{k}, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string', 'HeaderLines', startRow(block)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
for col=1:length(dataArray)
dataArray{col} = [dataArray{col};dataArrayBlock{col}];
ncol = col;
end
end
fclose(fileID{k});
%%Allocate imported array to column variable names
P = dataArray{:, 1};
R1 = dataArray{:, 2};
R2 = dataArray{:, 3};
T = dataArray{:, 4};
end

6 Commenti

Image Analyst
Image Analyst il 1 Gen 2018
Modificato: Image Analyst il 1 Gen 2018
You forgot to attach even a single .ASC file to allow people to help you.
All I might suggest until then is to look into readtable().
Uh oh. I just read again and found this "create a variable name for the corresponding column vectors that looks like filename_columnname for easy identification further." Really bad, BAD idea. Why? See the FAQ. I mean, how would you ever refer to them later in the code if you don't know what their names will be? It won't be easy like you think and is certainly not advisable.
I read many posts about how bad it would be. However, I will know the generated filename as it would have a pattern (i.e. 9_P or 22_R1). Basically to do processing on 101 files x 4 column each seems painful if I have to import each of the 101 files and create their variables separately...
It would be possible, but it would not be recommended.
You could use, for example,
all_data.(filename{k}) = data;
to put all of the data into a struct with field name which is the file name.
Her filenames start with a number. I know it's not allowed to have variable names start with a number. Are field names allowed to start with a number? I'd guess not. So if not, you'd have to prepend a letter:
fn = sprintf('a%s_R1', filename{k}) % Prepend letter a
all_data.(fn) = data;
Again, NOT recommended. Better to use an array.
Your edit of using a cell array is now much better than the other way of (trying/wanting to) using dynamically named variables.
Stephen23
Stephen23 il 3 Gen 2018
Modificato: Stephen23 il 3 Gen 2018
"I would like to create a variable name for the corresponding column vectors that looks like filename_columnname for easy identification further."
Do NOT do this. This would actually be slow, complex, and very buggy way to write your code. It would not be "easy", read this to know why:
Using indexing would be simple, neat, and very efficient. You should use one array and indexing. Indexing is the "easy" solution you should be using.

Accedi per commentare.

Risposte (0)

Categorie

Richiesto:

il 1 Gen 2018

Modificato:

il 3 Gen 2018

Community Treasure Hunt

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

Start Hunting!

Translated by