0

I have a csv file in with these column

column1      column2      column3
  1            2            3

and G.Edges in this format

   1            2           3         4 
EndNodes      Weight      Name     Distance 

what I would like to do is read the grocerydb.csv file and add all the data in column2 of the csv file to column 5 of G.Edges

i tried this code

 fid = fopen('grocerydb.csv');
 productUPC = textscan(fid,1);
 productName = textscan(fid,'%s',2);
 fclose(fid);

but it reads the second row not the column. I have review some answers to similar questions such as this Reading specific column from CSV file in matlab. which gave instruction for rows, what I want is a column data not row

how can i implement reading the second column and add it to G.Edges?

e.iluf
  • 1,389
  • 5
  • 27
  • 69
  • 1
    Possible duplicate of [Reading specific column from CSV file in matlab](https://stackoverflow.com/questions/17440689/reading-specific-column-from-csv-file-in-matlab) – Sardar Usama Dec 06 '17 at 06:30

1 Answers1

0

I assume that the G.Edges is a table in MATLAB for this makes life easier. As far as I know, MATLAB do not like csv files as Python do. So, we read csv as a table. Suppose that we have a table TARGETTable with variables acct, num_courses_visited, total_minutes_visited, and lessons_completed which like your G.Edges., Furthermore, there are many columns in a csv file ‘daily-engagement.csv’, we just need the data in second column utc_date. The csv file is very large, we need a few lines. At last, we note that the column utc_date is data, we should transform it to right type in MATLAB.

filename = 'D:\Temp\daily-engagement.csv';
delimiter = ',';
startRow = 2;
endRow = 9;
formatSpec = '%*q%{yyyy-MM-dd}D%*s%*s%*s%*s%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, endRow-startRow+1, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines', startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
fclose(fileID);
dailyengagement = table(dataArray{1:end-1}, 'VariableNames', {'utc_date'});

Now, we can combine this dailyengagement to TARGETTable.

TARGETTable = [TARGETTable dailyengagement]
Blue Bird
  • 318
  • 2
  • 9