How to create variable names from csv column names?

I am trying to automate the following, say I have a csv file:
--------------------
06/11/2013
ABBDC
Name Weight Age Sex
Jen 60 40 F
Tom 85 35 M
Siri 70 35 F
Sam 82 38 M
--------------------
Something like the following should work:
filename = ‘sample.csv';
formatSpec = '%s%s%s%s';
fileID = fopen(filename,'r');
Array = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(fileID);
delimiter = ',';
startRow = 4;
Name = Array{:, 1};
Weight = Array{:, 2};
Age = Array{:, 3};
Sex = Array{:, 4};
But I’m trying to get the variable names to be determined from the csv file. How is this possible in matlab?
I need this automated:
Name =[Jen Tom Siri Sam] %string
Weight = [60 85 70 82]
Age = [...]
Sex= [...]
I have found "Advanced: loading files using their names for variable names" from Doug Hull , very good! but that’s not what I need here.
And "For Matlab, Import data from xlsx, how to get 1st row as variable name, and rest of the column as data for variable name" from Shai, doesn’t work when I change to csvread or textread.

2 Commenti

If you know a priori the structure of the file, nothing prevents you to read data appropriately with TEXTSCAN, get an output cell array, and define variables..
data = textscan( .. ) ;
name = data{1} ;
weight = data{2} ;
...
Taking field names as variable names raises all sorts of issues, as MATLAB is case sensitive, has forbidden characters for variable names, etc. Moreover, if you don't know in advance the name of variables, you won't be able to use them later in the code. And if you do know them in advance, there is no need to read them from the file.
If the order of fields had to be determined though, you could read line 3 of the file, and then define a column ID per variable using STRCMPI. These IDs would also allow you to define the formatSpec string for TEXTSCAN.
For now the code from Azzi is a good start for me. I won't know the variable names/order in advance but each files "name" field (for example) won't change across the files. Thank you for your comment.

Accedi per commentare.

 Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 6 Nov 2013
Modificato: Azzi Abdelmalek il 6 Nov 2013
This a very bad idea,just let your data in one variable, in your case the variable Array.

3 Commenti

John
John il 7 Nov 2013
Modificato: John il 7 Nov 2013
Thank you Azzi that link is good! But the variable names are important to my application 1 variable will not be useful. I need the variable names for each column from the file.
you can use structure array
A=[1 2 3;4 5 6;7 8 9] % your matrix
names={'a','b','c'}
for k=1:numel(names)
data.(names{k})=A(:,1)
end
%or
A=[1 2 3;4 5 6;7 8 9] % your matrix
names={'a','b','c'}
for k=1:numel(names)
assignin('base',names{k},A(:,k))
end
Definitely useful. Thank you.

Accedi per commentare.

Più risposte (1)

I agree with Azzi. This is virtually never needed and to be avoided. How can you write and understand your program if the variables representing the columns are to be called Name, Weight, Age, and Sex one time, and var1, var2, var3, and var4 another time, and col1, col2, col3, col4 the next time? The only way is to use dynamic structure names but you're really heading into trouble and I recommend against it. There is no reason you can't call them the same thing every time you run the program, like col1, col2, and so on.

3 Commenti

The column names will be the same format. But the order may change. For example on 1 instance the file could be: name, dob, sex, weight.
On another instance the file could be: name, weight, sex, fav color. Note missing columns , new columns and different order. That's why I am heading down this path. Maybe I'll try a loop and search.
It's a bad path you're heading down, though you don't realize it. But anyway, Azzi gave you some code to do it. Yes, it's complicated but that's what you get when you do dynamic structure names. The code would be simpler if you did it like we recommend, since there is no reason for you to do it like you think you need to.
Thank you. I have accepted Azzi's answer.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by