From: Alessandra on 10 Jul 2010 04:49 Hi There, I cannot menage to convert an array cell into double. I'll expain you better. I imported a txt file of two coloums, the first is the problematic one, it looks like this: S 75 S100 S 50 S 1 S128 etc.. Some elements have a space, some two spaces some none. While importing I used textread and this syntax that allowed me to split the "numeric" part of these elements from the "Character" part. My two coloumns are stim and time: [trash, stim, time] = textread(files, '%c %s %f', 'delimiter','\t', 'whitespace', '', 'headerlines',2); clear trash This works out fine but one I have my stim array this is saved as cell and if I print it, it looks like this: '128 ' ' 1' ' 50' etc.. I need to convert this new array of only numbers although treated as cell into a double so that I can creat a matrix with stim and time as coloumn. I need this since the stim variable has markers that I need to filter the time variable. I hope it was clear, I can't find a solution for this, could you help me? Thanks a lot Ale
From: dpb on 10 Jul 2010 09:20 Alessandra wrote: > Hi There, > > I cannot menage to convert an array cell into double. .... > ... stim array ... as cell and ... looks like this: > > '128 ' > ' 1' > ' 50' > etc.. > > I need to convert ... into a double ... .... AFAIK there's still no way other than the loop solution... >> s={'128 ';' 1';' 50'}; >> v=zeros(size(s); >> v=zeros(size(s)); >> for idx=1:length(s), v(idx)=str2num(s{idx});end >> v v = 128 1 50 >> whos v Name Size Bytes Class v 3x1 24 double array Grand total is 3 elements using 24 bytes >> There are some more advanced cell operations/functions in releases later than mine so perhaps there is a solution...mayhaps one could now use cellfun()??? (but it's only an limited early precursor in R12 so can't experiment) --
From: Faraz Afzal on 10 Jul 2010 09:31 Alessandra <danda.galli(a)gmail.com> wrote in message <1075412655.5267.1278766191278.JavaMail.root(a)gallium.mathforum.org>... > Hi There, > > I cannot menage to convert an array cell into double. > I'll expain you better. I imported a txt file of two coloums, the first is the problematic one, it looks like this: > > S 75 > S100 > S 50 > S 1 > S128 > etc.. > > Some elements have a space, some two spaces some none. > While importing I used textread and this syntax that allowed me to split the "numeric" part of these elements from the "Character" part. > My two coloumns are stim and time: > > [trash, stim, time] = textread(files, '%c %s %f', 'delimiter','\t', 'whitespace', '', 'headerlines',2); > clear trash > > This works out fine but one I have my stim array this is saved as cell and if I print it, it looks like this: > > '128 ' > ' 1' > ' 50' > etc.. > > I need to convert this new array of only numbers although treated as cell into a double so that I can creat a matrix with stim and time as coloumn. I need this since the stim variable has markers that I need to filter the time variable. > > I hope it was clear, I can't find a solution for this, could you help me? > > Thanks a lot > > Ale Hi Alessandra, There u go... A = cat(1,stim{:}) A = double(E) Let me know if it worked.. Regards, Muhammad Faraz
From: Faraz Afzal on 10 Jul 2010 09:45 dpb <none(a)non.net> wrote in message <i19s7k$620$1(a)news.eternal-september.org>... > Alessandra wrote: > > Hi There, > > > > I cannot menage to convert an array cell into double. > ... > > ... stim array ... as cell and ... looks like this: > > > > '128 ' > > ' 1' > > ' 50' > > etc.. > > > > I need to convert ... into a double ... > ... > > AFAIK there's still no way other than the loop solution... > > >> s={'128 ';' 1';' 50'}; > >> v=zeros(size(s); > >> v=zeros(size(s)); > >> for idx=1:length(s), v(idx)=str2num(s{idx});end > >> v > v = > 128 > 1 > 50 > >> whos v > Name Size Bytes Class > > v 3x1 24 double array > > Grand total is 3 elements using 24 bytes > > >> > > There are some more advanced cell operations/functions in releases later > than mine so perhaps there is a solution...mayhaps one could now use > cellfun()??? (but it's only an limited early precursor in R12 so can't > experiment) > > -- Hi , OK my method won't work.. I just noticed u have data stored in cells as strings, in other case cell2mat would also work.. Now i would say they way told by dpb is one of the solutions... Regards, Muhammad Faraz
From: Faraz Afzal on 10 Jul 2010 10:27 Alessandra <danda.galli(a)gmail.com> wrote in message <1075412655.5267.1278766191278.JavaMail.root(a)gallium.mathforum.org>... > Hi There, > > I cannot menage to convert an array cell into double. > I'll expain you better. I imported a txt file of two coloums, the first is the problematic one, it looks like this: > > S 75 > S100 > S 50 > S 1 > S128 > etc.. > > Some elements have a space, some two spaces some none. > While importing I used textread and this syntax that allowed me to split the "numeric" part of these elements from the "Character" part. > My two coloumns are stim and time: > > [trash, stim, time] = textread(files, '%c %s %f', 'delimiter','\t', 'whitespace', '', 'headerlines',2); > clear trash > > This works out fine but one I have my stim array this is saved as cell and if I print it, it looks like this: > > '128 ' > ' 1' > ' 50' > etc.. > > I need to convert this new array of only numbers although treated as cell into a double so that I can creat a matrix with stim and time as coloumn. I need this since the stim variable has markers that I need to filter the time variable. > > I hope it was clear, I can't find a solution for this, could you help me? > > Thanks a lot > > Ale Finally A better solution is to directly extract numerical data from text and then use catenation.. fid = fopen('YOUR_FILE.txt'); A = textscan(fid, ' %1s %f32 ') fclose(fid) and A{2} contains all numeric data not strings... Regards, Muhammad Faraz
|
Next
|
Last
Pages: 1 2 3 4 Prev: Reading Serial data through Simulink Input Packet Next: remove duplicate rows in sparse matrix |