how to rearrage a cell array?

4 views (last 30 days)
yousaf obaid
yousaf obaid on 20 Jun 2016
Commented: yousaf obaid on 20 Jun 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 Comments
yousaf obaid
yousaf obaid on 20 Jun 2016
@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.

Sign in to comment.

Accepted Answer

Shameer Parmar
Shameer Parmar on 20 Jun 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 Comment
yousaf obaid
yousaf obaid on 20 Jun 2016
Edited: yousaf obaid on 20 Jun 2016
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

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 20 Jun 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.
  1 Comment
yousaf obaid
yousaf obaid on 20 Jun 2016
@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.

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!