how to rearrage a cell array?

조회 수: 21 (최근 30일)
yousaf obaid
yousaf obaid 2016년 6월 20일
댓글: yousaf obaid 2016년 6월 20일
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
Stephen23
Stephen23 2016년 6월 20일
편집: Stephen23 2016년 6월 20일
yousaf obaid
yousaf obaid 2016년 6월 20일
@Stephen Cobeldick i know this question seems like a duplicate to the one mentiond by you and it kind of is i admit, but the output of the previous question answered by Guillaume was not entirely what i needed. this is a little different from my previous question. i needed it to be done badly thats why i posted this again. i hope you understand the Scenario. Thank you.

댓글을 달려면 로그인하십시오.

채택된 답변

Shameer Parmar
Shameer Parmar 2016년 6월 20일
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
yousaf obaid
yousaf obaid 2016년 6월 20일
편집: yousaf obaid 2016년 6월 20일
Works like Magic. Thank you so much sir. I have one more question. could you please take one more look on the text file and tell me how to get the date and time columns as well? For example;
EDIT: do let me know if this demands a seperate question. Thank you.
Date Time 06 00 06 00
OBDMIDs ExhaustGasSensorMonitorBank1Sensor1
2016-03-18 09:21:31 OBDmonitorIDsSupported$01_$1F Supported

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2016년 6월 20일
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.
  댓글 수: 1
yousaf obaid
yousaf obaid 2016년 6월 20일
@guillaume hello. i did not discard your solution. it was almost working fine but i told you on your answer that it still needed some tweaks here and there. As you may have noticed that this solution is using your technique only. besides i needed more work to be done on it as in to get the date and time columns and the names of all the text files present in the main folder i hope that you understand the situation. and i apoligize if you feel bad in anyway because of me. I am sorry.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by