How to create variable names from csv column names?
Show older comments
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 Comments
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.
John
on 8 Nov 2013
Accepted Answer
More Answers (1)
Image Analyst
on 7 Nov 2013
0 votes
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 Comments
John
on 7 Nov 2013
Image Analyst
on 7 Nov 2013
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.
John
on 8 Nov 2013
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!