开发者

Read a CSV file in matlab and make it 2-d

开发者 https://www.devze.com 2023-04-05 01:53 出处:网络
This is an example of what I want to do: I have a CSV file: 1,200,3,500... 2,400,4,600... Data are paired, i.e. there are alternating values x,y,x,y….

This is an example of what I want to do:

I have a CSV file:

1,200,3,500...

2,400,4,600...

Data are paired, i.e. there are alternating values x,y,x,y….

I want to take this input and make a 2d matrix where the matrix value is m = x–y. Output shou开发者_运维技巧ld be in CSV format with triples x,y,m,x,y,m,…. Any help is appreciated.


The solution by @VladimirPerković had the right idea. Let me just fix some minor issues:

%# read CSV file
data = csvread('file.csv');
[r c] = size(data);

%# create output matrix
out = zeros(r,c/2*3);
out(:,1:3:end) = data(:,1:2:end);
out(:,2:3:end) = data(:,2:2:end);
out(:,3:3:end) = data(:,1:2:end) - data(:,2:2:end)

%# save as CSV file
csvwrite('out.csv', out)

The output file created:

1,200,-199,3,500,-497,1,200,-199,3,500,-497
2,400,-398,4,600,-596,2,400,-398,4,600,-596
1,200,-199,3,500,-497,1,200,-199,3,500,-497
2,400,-398,4,600,-596,2,400,-398,4,600,-596


This is not a problem:

data = importdata('file.csv');
x = data(1:2:end); //number in the middle means you take every second sample
y = data(2:2:end);
m = x - y;

Now the output:

output_data = [x;y;m];
output_data = output_data(:);
csvwrite ('output.csv' , output_data);
0

精彩评论

暂无评论...
验证码 换一张
取 消