Editing the cell array

25 views (last 30 days)
yousaf obaid
yousaf obaid on 10 Jun 2016
Commented: yousaf obaid on 15 Jun 2016
my cell array looks like this; %original file is also attached below.
'1.1 ECU Identification (Ford Escape Initial)'
'============================================'
'PDU Parameter Value Unit'
'----------------------------------------------------------------------------------------------------'
'06 00 OBDMIDs OBDmonitorIDsSupported$01_$1F '
' ExhaustGasSensorMonitorBank1Sensor1 supported '
' ExhaustGasSensorMonitorBank1Sensor2 supported '
'06 01 Monitor ID ExhaustGasSensorMonitorBank1Sensor1 '
' testValue 0.009 s'
' minimumTestLimit 0.000 s'
' maximumTestLimit 0.400 s'
' Test ID 0x87 '
' Monitor ID ExhaustGasSensorMonitorBank1Sensor1 '
' testValue 0.020 s'
' minimumTestLimit 0.000 s'
' maximumTestLimit 0.400 s'
' Test ID 0x88 '
'06 02 Monitor ID ExhaustGasSensorMonitorBank1Sensor2 '
' testValue -7876 mV/s'
' minimumTestLimit -30000 mV/s'
' maximumTestLimit 0 mV/s'
' Test ID 0x85 '
' Monitor ID ExhaustGasSensorMonitorBank1Sensor2 '
' testValue 1.889 s'
' minimumTestLimit 0.000 s'
' maximumTestLimit 6.000 s'
' Test ID 0x86 '
'06 20 OBDMIDs OBDmonitorIDsSupported$21_$3F '
' CatalystMonitorBank1 supported '
' VVTMonitorBank1 supported '
' EVAPmonitorCapOff not supported '
' EVAPmonitor_0_090 supported '
' EVAPmonitor_0_040 supported '
' EVAPmonitor_0_020 supported '
' PurgeFlowMonitor supported '
'06 21 Monitor ID CatalystMonitorBank1 '
' testValue 0.062499 ratio'
' minimumTestLimit 0.000000 ratio'
' maximumTestLimit 0.761709 ratio'
' Test ID 0x81 '
'06 3A Monitor ID EVAPmonitor_0_090 '
' testValue -23.25 Pa'
' minimumTestLimit -996.25 Pa'
' maximumTestLimit 8191.75 Pa'
' Test ID 0x80 '
' Monitor ID EVAPmonitor_0_090 '
' testValue -1992.50 Pa'
' minimumTestLimit -8192.00 Pa'
' maximumTestLimit -1992.50 Pa'
' Test ID 0x82 '
'06 3B Monitor ID EVAPmonitor_0_040 '
What i need to do is to rearrange it in such a way;
File 06 01 06 01 06 01 06 01 06 01 06 02..
Monitor ID testValue minTestLimit maxTestLimit Test ID ...
Test1 ExhaustGasSensorMonitorBank1Sensor1 0.009 0.000 0.400 0x87 ...
and so on. i think i need to loop through all the cell array to get the desired formation but i dont know how to do it. This is what i have done till now.
pwd %identify current Folder
path=fullfile(pwd,'*.txt'); %looks for text files
dir(path) %displays the text file
path=uigetdir(); %open folder selection dialog box
A=genpath(path); %generates path string, returns all folders and subfolders
B=strread(A,'%s','delimiter',';'); %creates cell Array
for p=1:numel(B) %lists all the text files
file=dir(fullfile(B{p},'*.txt'));
end
Cstr = textread('Test1.txt', '%s', 'delimiter', ''); %read file into cell array of strings
Any help in this regard is greatly appreciated. Thank you.
  3 Comments
Stephen23
Stephen23 on 15 Jun 2016
Edited: Stephen23 on 15 Jun 2016
@yousaf obaid: presumably that data is being read from some data file. It might be easier if you edit your question and upload the file so that we can run our own code on it. This makes helping you much easier.
Probably you do not want the output to be a cell array, but some new file. Please explain a bit more what you are actually trying to achieve, because there may be easier ways to achieve it than simply "rearranging" a cell array.
yousaf obaid
yousaf obaid on 15 Jun 2016
I have uploaded the text file. please have a look at it. and what i actually want is to rearrange the cell array as shown in the question and then store the rearranged data on an Excel sheet. Now i am not certain if the output would be some new file or a new cell array but as long as it gets stored in the Excel sheet in desired format.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 15 Jun 2016
I'm sure there is a way for textscan to read your file in one go, but I always struggle to create a format string that works. Particularly, in your case your file has fixed width columns except for the last field (unit) which has varying width.
No matter, since you've already read the file let's start with your cell array. First, get rid of the headers:
%c: your cell array. I assume it's a 51x1 cell array
c = c(5:end);
Then transform into a char array. This will ensure that all fields, including the units have the same with. char will just pad the end of each line with spaces:
c = char(c);
It's then easy to split the columns, using fixed width, and transform back into cell arrays. Note that cellstr automatically get rid of the extra spaces in each field.
%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));
The only thing left is to replicate the PDU for those rows where it is omitted. You could simply use a loop or:
ispresent = ~cellfun(@isempty, pdu);
pdu = repelem(pdu(ispresent), diff(find([ispresent; true])));
Your desired output is then
newc = [pdu'; parameter'; value']
  4 Comments
Guillaume
Guillaume on 15 Jun 2016
I don't know how you've generated the cell array but if I just copy the values you've put in your initial post, my code works properly. Note that I got the width of the columns from your text file and your cell array appears to be a proper match for that.
A simple way to regenerate that cell array without the headers would be:
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
After that my code produces the right result.
If repelem is undefined that is because you're using an older version of matlab (always a good to say so). repelem appeared in R2015a. You can replace that part with:
ispresent = ~cellfun(@isempty, pdu);
pduvalid = pdu(ispresent);
pdu = pduvalid(cumsum(ispresent))
yousaf obaid
yousaf obaid on 15 Jun 2016
You are right about using the older version of matlab. I have R2013a. The alternate that you suggested for repelem is working as well. But the desired cell array is still a little out of place here and there. Anyway I thank you for the detailed answer. It was really helpful, was totally stuck before your answer but now i can move forward on this task. Thanks once again.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Identification 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!