how to rearrage a cell array?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
yousaf obaid
el 20 de Jun. de 2016
Comentada: yousaf obaid
el 20 de Jun. de 2016
I have a 2x219 cell array which looks like this;
06 00 OBDMIDs ExhaustGasSensorMonitorBank1Sensor1 ExhaustGasSensorMonitorBank1Sensor1
OBDmonitorIDsSupported$01_$1F supported supported
and so on. 06 00 OBDMIDs is one column. Is there a possibility where i can convert this 2 row array into a 3 row array? For example like this;
06 00 06 00 06 00
OBDMIDs ExhaustGasSensorMonitorBank1Sensor1 ExhaustGasSensorMonitorBank1Sensor2
OBDmonitorIDsSupported$01_$1F supported supported
so what i basically want is to keep the 06 00 in first row and move the corresponding values one row down each. this is what i have done till now;
Cstr = textread('Test1.txt', '%s', 'delimiter', '');
cString = Cstr(29:end); %removes headers
cString = char(cString); %Transforms into a char array,...
%split the columns
pdu = cellstr(cString(:, 1));
parameter = cellstr(cString(:, 1:42));
value = cellstr(cString(:, 43:86));
%unit = cellstr(cString(:, 87:end));
ispresent = ~cellfun(@isempty, pdu); %pdu for the rows where it is omitted from
pduvalid = pdu(ispresent);
pdu = pduvalid(cumsum(ispresent));
newc = [pdu'; parameter'; value']; %desired output
row = 1;
newc(row,:) = [];
The original text file and the cell array excel sheet is attached below. I know its just a small problem but somehow i am stuck on it. Thank you for any kind of help in this regard.
2 comentarios
Respuesta aceptada
Shameer Parmar
el 20 de Jun. de 2016
Here is the code:
Once you calculate the variables 'parameter' and 'value'
Below that add this code..
% value of variable 'parameter' and 'value' should be available
for i=1:length(parameter)
temp = parameter{i};
if ismember(temp(1),num2str(0:9))
tempSplit = regexp(temp,' ', 'split');
newPDU(i) = tempSplit(1);
newParameter(i) = tempSplit(2);
else
newPDU(i) = newPDU(i-1);
newParameter(i) = parameter(i);
end
end
newc = [newPDU; newParameter; value'];
try for this...
1 comentario
yousaf obaid
el 20 de Jun. de 2016
Editada: yousaf obaid
el 20 de Jun. de 2016
Más respuestas (1)
Guillaume
el 20 de Jun. de 2016
I don't really understand, I gave you a solution that worked perfectly well, and produced the exact result you asked above, and you went and discarded it and are now using a lot more convoluted solution.
For reference:
c = strsplit(fileread('test1.txt'), {'\r', '\n'}); %read whole file in one go and split along line ends.
c = c(29:end-1); %get rid of the header and final blank line
c = char(c);
%column 1 to 7 is pdu, 8 to 50 is parameter, 51 to 93 is value, 94 to end is unit
pdu = cellstr(c(:, 1:7));
parameter = cellstr(c(:, 8:50));
value = cellstr(c(:, 51:93));
unit = cellstr(c(:, 94:end));
ispresent = ~cellfun(@isempty, pdu);
pduvalid = pdu(ispresent);
pdu = pduvalid(cumsum(ispresent))
newc = [pdu'; parameter'; value']
No loop needed, no expensive num2str conversion, and a very logical parsing.
Ver también
Categorías
Más información sobre Function Creation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!