MATLAB: I want to change the first number of each cell without changing the other numbers. C is 3*1 cell. For example change 32 to 65.

cell arrays

C={'1 555.00 554.10 552. 550.2 549. 545.59';
'32 553.38 552.40 550.3 548.91 547.83 545.59';
'91 552.64 551.70 549. 546.78 545.6 545.59'}

Best Answer

>> C = {'1 555.00 554.10 552. 550.2 549. 545.59';
'32 553.38 552.40 550.3 548.91 547.83 545.59';
'91 552.64 551.70 549. 546.78 545.6 545.59'}
C =
'1 555.00 554.10 552. 550.2 549. 545.59'
'32 553.38 552.40 550.3 548.91 547.83 545.59'
'91 552.64 551.70 549. 546.78 545.6 545.59'
>> new = {'0';'64';'99'};
>> cellfun(@(s,n)regexprep(s,'^\d+',n),C,new,'Uni',0)
ans =
'0 555.00 554.10 552. 550.2 549. 545.59'
'64 553.38 552.40 550.3 548.91 547.83 545.59'
'99 552.64 551.70 549. 546.78 545.6 545.59'
Storing this data as strings is not going to be very efficient when doing these kind of operations on it. Much simpler would be to import that data as numeric data, and then these kind of operations are much simpler.