Data manipulation problem?

1 view (last 30 days)
CHANDAN PANDEY
CHANDAN PANDEY on 3 Apr 2019
Answered: Peter Perkins on 9 Apr 2019
I'm new to MATLAB, thanks in advance. I have a data file containing 2 columns and 76800 rows. How can I change(put the vaue in 2ndcolumn) to a new column everytime the 1st colmn data reaches 0. i want to save the result in a data.xls file.
I can read the contents of the file, but after that how to split the second column into multiple column. the 1st column data repeats itself for different values in second column. The file is attached.
X = dlmread('data');
x = X(:,1);
y = X(:,2);
n = numel(x);

Answers (2)

Andrei Bobrov
Andrei Bobrov on 3 Apr 2019
Edited: Andrei Bobrov on 3 Apr 2019
d = fopen('Data.csv');
c = textscan(d,'%s','delimiter','\n');
fclose(d);
c=c{:};
x = regexp(c,'\-?\d+(\.\d+)?([Ee][+-]\d+)?','match');
x1 = x(~cellfun(@isempty,x));
x1 = str2double(cat(1,x1{:}));
ii = find(x1(:,1) == 0);
[a,~,cc] = unique(diff(ii));
if numel(a) == 1
out = reshape(x1(:,2),a,[]);
else
out = accumarray(cc,x1(:,2),[],@(x){x});
end

Peter Perkins
Peter Perkins on 9 Apr 2019
Definitely move away from dlmread. fopen+textscan are very powerful, but just make more work in this case.
Use readtable. You have NaNs to delimit the cycles, get rid of those.
>> t = readtable('data.csv');
>> t(isnan(t.Var1),:) = [];
64 cycles of 1200 rows, right?
>> n = sum(t.Var1 == 0)
n =
64
>> m = height(t) / n
m =
1200
Add a dummy variable to the table, and it becomes a one-liner using unstack:
>> t.Cycle = repelem(1:n,m)';
>> t2 = unstack(t,"Var2","Cycle",'GroupingVariable',"Var1");
Warning: Table variable names were modified to make them valid MATLAB identifiers.
>> head(t2)
ans =
8×65 table
Var1 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 x31 x32 x33 x34 x35 x36 x37 x38 x39 x40 x41 x42 x43 x44 x45 x46 x47 x48 x49 x50 x51 x52 x53 x54 x55 x56 x57 x58 x59 x60 x61 x62 x63 x64
__________ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _________ _________ _________ _________ _________ ______ ______ ______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _________ _________ _________ _________ _________ _________ _________ _________ ___________ ________ ________ ______ ______ ______ ______ ______ ______ ______ ______ ______ _______ _______ _______ _______ _______ _______
0 -8.6686 -5.1262 -5.037 -4.9773 -4.9382 -3.4405 -3.321 -2.8829 -2.8829 -2.8356 -2.592 -2.592 -2.3489 -2.3489 -1.84 -1.8275 -1.7243 -1.7232 -0.739504 -0.739504 -0.716604 -0.692504 -0.454904 9.2626 9.2727 9.2727 14.7908 14.8477 16.042 16.042 16.2395 16.2395 -8.5462 -4.4571 -4.3184 -3.2119 -3.1809 -1.4238 -0.880204 -0.878904 -0.864304 -0.864304 -0.825204 -0.825204 -0.080304 -0.020104 -0.020104 0.873796 0.995396 1.0417 1.4168 1.7259 1.7633 1.7633 2.3129 9.5007 9.5007 9.5167 15.7309 15.7387 16.6355 16.6355 16.8591 16.8591
0.00087944 -8.6685 -5.1262 -5.0369 -4.9772 -4.9381 -3.4405 -3.3211 -2.8829 -2.8829 -2.8356 -2.5922 -2.5919 -2.3493 -2.3488 -1.8397 -1.8276 -1.7243 -1.723 -0.739604 -0.739504 -0.716604 -0.692604 -0.454904 9.2628 9.2716 9.2729 14.791 14.8484 16.0365 16.0421 16.2346 16.2397 -8.546 -4.4571 -4.3183 -3.2119 -3.1809 -1.4239 -0.881504 -0.880204 -0.864304 -0.864204 -0.825304 -0.822504 -0.084104 -0.020004 -0.016304 0.873996 0.995396 1.0416 1.4168 1.7258 1.7632 1.7633 2.3129 9.4998 9.501 9.5169 15.7311 15.7392 16.6276 16.6355 16.8547 16.8593
0.00175888 -8.6681 -5.126 -5.0368 -4.977 -4.938 -3.4406 -3.3214 -2.8829 -2.8828 -2.8356 -2.5928 -2.5915 -2.3503 -2.3486 -1.8387 -1.8277 -1.7243 -1.7225 -0.739904 -0.739604 -0.716404 -0.692904 -0.454704 9.2635 9.2683 9.2736 14.7918 14.8505 16.0205 16.0424 16.2201 16.24 -8.5457 -4.4568 -4.3181 -3.2118 -3.1807 -1.4241 -0.888204 -0.880204 -0.864304 -0.864104 -0.825704 -0.815604 -0.093604 -0.019704 -0.00680401 0.874296 0.995396 1.0415 1.4168 1.7254 1.7628 1.7632 2.3129 9.4971 9.5018 9.5178 15.7315 15.741 16.6055 16.6358 16.8416 16.8596
0.00263832 -8.6675 -5.1258 -5.0365 -4.9768 -4.9379 -3.4408 -3.3218 -2.8829 -2.8826 -2.8356 -2.5937 -2.5908 -2.3519 -2.3481 -1.837 -1.8278 -1.7243 -1.7215 -0.740304 -0.739804 -0.716204 -0.693304 -0.454504 9.2627 9.2647 9.2748 14.7929 14.854 15.995 16.043 16.1968 16.2406 -8.545 -4.4565 -4.3177 -3.2115 -3.1804 -1.4244 -0.897204 -0.880304 -0.864304 -0.864004 -0.826304 -0.806204 -0.105904 -0.019204 0.00549599 0.874896 0.995396 1.0413 1.417 1.7247 1.7621 1.763 2.3129 9.4926 9.5032 9.5191 15.7321 15.7442 16.5721 16.6363 16.8205 16.8602
0.00351776 -8.6666 -5.1254 -5.0362 -4.9764 -4.9376 -3.441 -3.3224 -2.8829 -2.8824 -2.8356 -2.595 -2.5898 -2.3543 -2.3475 -1.8347 -1.8281 -1.7242 -1.7202 -0.741004 -0.740004 -0.715904 -0.694004 -0.454304 9.255 9.2663 9.2764 14.7944 14.859 15.9615 16.0437 16.1658 16.2414 -8.5441 -4.456 -4.3172 -3.2112 -3.18 -1.4248 -0.907304 -0.880304 -0.864404 -0.863704 -0.827204 -0.795604 -0.119504 -0.018604 0.019196 0.875696 0.995396 1.041 1.4171 1.7238 1.7612 1.7627 2.313 9.4863 9.5051 9.521 15.7326 15.7488 16.5308 16.637 16.7923 16.8611
0.0043972 -8.6654 -5.125 -5.0357 -4.9759 -4.9373 -3.4414 -3.3231 -2.8829 -2.8821 -2.8356 -2.5967 -2.5885 -2.3572 -2.3467 -1.8318 -1.8284 -1.7242 -1.7186 -0.741904 -0.740304 -0.715504 -0.694904 -0.453904 9.2451 9.2684 9.2785 14.796 14.8657 15.9212 16.0447 16.1282 16.2425 -8.543 -4.4553 -4.3166 -3.2108 -3.1795 -1.4253 -0.918004 -0.880404 -0.864404 -0.863404 -0.828204 -0.784304 -0.133804 -0.017704 0.033596 0.876696 0.995396 1.0406 1.4173 1.7226 1.76 1.7624 2.313 9.4782 9.5075 9.5235 15.733 15.755 16.4837 16.6379 16.7578 16.8622
0.00527665 -8.664 -5.1244 -5.0351 -4.9753 -4.937 -3.4418 -3.324 -2.8829 -2.8817 -2.8356 -2.5987 -2.5869 -2.3608 -2.3457 -1.8287 -1.8283 -1.7242 -1.7165 -0.742904 -0.740704 -0.715004 -0.695904 -0.453404 9.2331 9.271 9.2811 14.7977 14.8741 15.8756 16.0459 16.0852 16.2438 -8.5416 -4.4546 -4.3158 -3.2103 -3.1789 -1.426 -0.928904 -0.880504 -0.864504 -0.863104 -0.829604 -0.772604 -0.148504 -0.016704 0.048396 0.877996 0.995396 1.0402 1.4176 1.7212 1.7585 1.762 2.3131 9.4684 9.5105 9.5264 15.7335 15.7626 16.4327 16.6389 16.7181 16.8635
0.00615609 -8.6624 -5.1237 -5.0344 -4.9746 -4.9366 -3.4422 -3.325 -2.883 -2.8813 -2.8356 -2.6011 -2.5851 -2.365 -2.3445 -1.8292 -1.8242 -1.7241 -1.7141 -0.744204 -0.741104 -0.714404 -0.697204 -0.452904 9.2189 9.274 9.2841 14.7994 14.8843 15.8257 16.0378 16.0472 16.2453 -8.5399 -4.4536 -4.3149 -3.2097 -3.1782 -1.4268 -0.940004 -0.880604 -0.864604 -0.862604 -0.831104 -0.760504 -0.163404 -0.015504 0.063396 0.879496 0.995396 1.0396 1.4179 1.7194 1.7568 1.7615 2.3132 9.4568 9.514 9.53 15.734 15.7716 16.3787 16.6402 16.674 16.8651
I'm gonna guess that the first column in your file are elapsed times, so take a look at timetables. Just a guess.

Categories

Find more on Tables 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!