help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to compare the colums ( 2nd and 3rd) of 2 .csv files based on a


From: Ganesh Kini
Subject: Re: How to compare the colums ( 2nd and 3rd) of 2 .csv files based on a condition
Date: Wed, 24 Jun 2020 11:10:32 +0200

results: for file 1
 [1,1] =
  {
    [1,1] = tb
    [2,1] = tb
    [3,1] = tb
    [4,1] = ta
    [5,1] = ta
    [6,1] = tc 
    [7,1] = tc
    [8,1] = tc  
}

condition1 = cell2mat(data1{1})

error 
error: cat: dimension mismatch
error: called from
    cell2mat at line 80 column 11
    fileoopencheck at line 27 column 12


On Wed, Jun 24, 2020 at 2:37 AM Nicholas Jankowski <jankowskin@asme.org> wrote:
On Tue, Jun 23, 2020 at 8:08 PM Ganesh Kini <ganeshrkini19@gmail.com> wrote:
Yes I used 
data1 = textscan(file1, '%s,%f,%f,%d\n', 'HeaderLines', 1, 'Delimiter',',')


  and did adding the \r help or make any difference? and you tested on the exact same csv files you sent? i'm not sure why the exact same thing wouldn't work. 
 
But again I want to get the difference between the 2nd and 3rd column based on condition and number. How do I map it and then compare the value?  

Eg: condition ta and  number 1 of the 4th row of file1 has to compare with  condition ta and  number 1 of the 1st row of file2 

Once you solve the problem reading in the data, you can pull subsets and do subtraction of the data.  

if you prefer to work with named entities, you can pull them out of the cells:

file1 = fopen('file1.csv');
file2 = fopen('file2.csv');
data1 = textscan(file1, "%s,%f,%f,%d\n", "HeaderLines", 1, "Delimiter",",");
data2 = textscan(file2, "%s,%f,%f,%d\n", "HeaderLines", 1, "Delimiter",",");
fclose(file1);
fclose(file2);

condition1 = cell2mat(data1{1});  ##textscan made the char array into a cell array, so cell2mat pulls it back out.
supply1_1 = data1{2};
supply2_1 = data1{3};
number1 = data1{4};

condition2 = cell2mat(data2{1});
supply1_2 = data2{2};
supply2_2 = data2{3};
number2 = data2{4};

then, you use logical indexing to pull out subsets based on the values of condition1 and 2:

>> pos_a_1 = (condition1(:,2)=='a')
pos_a_1 =

  0
  0
  0
  1
  1
  0
  0
  0

>> pos_a_2 = (condition2(:,2)=='a')
pos_a_2 =

  1
  1
  0
  0
  0
  0
  0
  0

>> supply1_1(pos_a_1)
ans =

   1.2700
   1.3700

>> supply1_2(pos_a_2)
ans =

   5.010000
   0.070000

>> supply1_1(pos_a_1) - supply1_2(pos_a_2)
ans =

  -3.7400
   1.3000

repeat for tb and tc.  perform whatever math you want, and store and save the answers however you want. 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]