Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
read text with double quote " " in one cell

Subject: read text with double quote " " in one cell

From: Snow

Date: 25 Jun, 2012 16:23:06

Message: 1 of 5

Hello all, I need some help with text imput. Here is an example of the text file

"datatitle","data1","data2","data3","data4","data5","data6"
"entry1",0,"",10,"off","NA",0
"entry2",2,"2,3,4",10,"on",0,0
"entry3",0,"",10,"off","NA",0

it is just an example but very similar to the original file, sometimes there is no data in one "space" so it is "", sometimes it shows "NA" or have multiple numbers separated by comma. What I am trying to get is cell array that looks like below:

"datatitle" "data1" "data2" "data3" "data4" "data5" "data6"
"entry1" 0 "" 10 "off" "NA" 0
"entry2" 2 "2,3,4" 10 "on" 0 0
"entry3" 0 "" 10 "off" "NA" 0

1. I have imported text file line by line using textread,please let me know if you think I should import with other command
2. I have tried to parse each line with regexp, but it would parse the "2,3,4" into 3 cells "2 | 3 | 4". I really need to get them into the same cell/string. Ideally i would wanna take average of the 3 numbers.
3. The number of columns keeps changing because I am looping through different files, so some might have have data1 through 6 but another file might have additional columns in the the middle. So textscan did not work for me since I would have to specify the format of each part I want to parse out.
4. I could try to use findstr to find all " in the line and try to see if there is comma in between 2 " and try to get rid of the " quote then parse again, but I really wish there is a better way to do this!

I hope I described it clear. Please help me out! Thanks a lot!

Subject: read text with double quote " " in one cell

From: KJ

Date: 25 Jun, 2012 20:44:33

Message: 2 of 5

first of all, is that possible to replace the deliminator ',' by space or tab?
then the regx could be able to do your job.

And after replacing the deliminator, you can use dlmread to read that file in.

hope it helps!

-KJ
 
On Monday, June 25, 2012 12:23:06 PM UTC-4, Snow wrote:
> Hello all, I need some help with text imput. Here is an example of the text file
>
> "datatitle","data1","data2","data3","data4","data5","data6"
> "entry1",0,"",10,"off","NA",0
> "entry2",2,"2,3,4",10,"on",0,0
> "entry3",0,"",10,"off","NA",0
>
> it is just an example but very similar to the original file, sometimes there is no data in one "space" so it is "", sometimes it shows "NA" or have multiple numbers separated by comma. What I am trying to get is cell array that looks like below:
>
> "datatitle" "data1" "data2" "data3" "data4" "data5" "data6"
> "entry1" 0 "" 10 "off" "NA" 0
> "entry2" 2 "2,3,4" 10 "on" 0 0
> "entry3" 0 "" 10 "off" "NA" 0
>
> 1. I have imported text file line by line using textread,please let me know if you think I should import with other command
> 2. I have tried to parse each line with regexp, but it would parse the "2,3,4" into 3 cells "2 | 3 | 4". I really need to get them into the same cell/string. Ideally i would wanna take average of the 3 numbers.
> 3. The number of columns keeps changing because I am looping through different files, so some might have have data1 through 6 but another file might have additional columns in the the middle. So textscan did not work for me since I would have to specify the format of each part I want to parse out.
> 4. I could try to use findstr to find all " in the line and try to see if there is comma in between 2 " and try to get rid of the " quote then parse again, but I really wish there is a better way to do this!
>
> I hope I described it clear. Please help me out! Thanks a lot!

Subject: read text with double quote " " in one cell

From: Snow

Date: 25 Jun, 2012 21:47:07

Message: 3 of 5

But then it will replace the comma between quotes"" as space/tab as well, then regexp would still give a 1*3 cell that looks like {' "2', '3', '4" '}


Snow


KJ <likejie@gmail.com> wrote in message <c7e5e5b9-fbb8-48d0-b729-2f76d9b165a5@googlegroups.com>...
> first of all, is that possible to replace the deliminator ',' by space or tab?
> then the regx could be able to do your job.
>
> And after replacing the deliminator, you can use dlmread to read that file in.
>
> hope it helps!
>
> -KJ
>

Subject: read text with double quote " " in one cell

From: KJ

Date: 25 Jun, 2012 23:10:32

Message: 4 of 5

could you provide your regexp code? over there the deliminator should also be space or tab

On Monday, June 25, 2012 5:47:07 PM UTC-4, Snow wrote:
> But then it will replace the comma between quotes"" as space/tab as well, then regexp would still give a 1*3 cell that looks like {' "2', '3', '4" '}
>
>
> Snow
>
>
> KJ wrote in message ...
> > first of all, is that possible to replace the deliminator ',' by space or tab?
> > then the regx could be able to do your job.
> >
> > And after replacing the deliminator, you can use dlmread to read that file in.
> >
> > hope it helps!
> >
> > -KJ
> >

Subject: read text with double quote " " in one cell

From: Phil Goddard

Date: 26 Jun, 2012 01:15:07

Message: 5 of 5

There's undoubtedly a more efficient way, but the following should do,

%%%%%%%%%%%%%Cut Here%%%%%%%%%%%%%
data = fileread('test.txt'); % read data
nrows = length(regexp(data,'\s*','split')); % determine number of rows
longList = regexprep(data,'\s*',','); %make one long comma separated string
tokens = regexp(longList,',','split'); % separate the list
ndq = cellfun(@numel,strfind(tokens,'"')); % get number of double quotes in each string
loc = find(ndq==1); % find location of cells with only one double quote

% loop and combine the cells between those with only one double quote
for idx = length(loc):-2:2
    str = sprintf('%s,',tokens{loc(1):loc(2)}); % combine cells
    tokens{loc(idx-1)} = str(1:end-1); % remove extra comma
    tokens(loc(idx-1)+1:loc(idx)) = []; % remove bad cells
end

% Reshape to get result
myCell = reshape(tokens,nrows,length(tokens)/nrows);
%%%%%%%%%%%%%Cut Here%%%%%%%%%%%%%

Phil.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us