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:
cell array dimensions do not match

Subject: cell array dimensions do not match

From: Alex

Date: 12 Dec, 2012 23:21:06

Message: 1 of 4

I am having a problem where I am trying to create a cell array of strings to use in rmfield. I am combining a stem(chart) with a number (any number) to create the field I want to remove. For example, if I need to remove DeltaT.chart1, deltaT.chart6, deltaT.chart7, then my cell array would be:
chart1
chart6
chart7

The problem I have is when I get to chart 10 or more. If I have a number greater than 10 I would have the following:
chart1
chart6
chart7
chart10
and I get the error
??? Error using ==> vertcat
CAT arguments dimensions are not consistent.

Error in ==> remover at 28
            toremove = [toremove;field]

I think because I am trying to fit a 7 character wide (chart10) string into a 6 character wide cell array.

Is there a way around this or something I can do instead? My code is below if it helps.

Thanks,
Alex




for a = 17%1:22
    listremove = []
    toremove = []
    rmvr = 0;
    Fileone = 'NODIPSinBTBTB_Delta';
    File = [Fileone,'Comp',int2str(a)]
    
    load([Fileone,int2str(a),'.mat'])
    
    data = deltaT;
    stem = 'chart';
    
    for L = 1:length(fieldnames(data))
        field = [stem,int2str(L)];
        
        if length(data.(field)) == 0
            rmvr = 1
            listremove = [listremove L];
        end
    end
    if rmvr == 1
        for g = 1:length(listremove)
            field = [stem,int2str(listremove(g))]
            toremove = [toremove;field]
        end
        deltaT = rmfield(data, toremove)
    end
    
    save(File, 'deltaT')
    
    clear all

end

Subject: cell array dimensions do not match

From: bartekltg

Date: 12 Dec, 2012 23:41:14

Message: 2 of 4

W dniu 2012-12-13 00:21, Alex pisze:
> I am having a problem where I am trying to create a cell array of

So try cell array:)

> strings to use in rmfield. I am combining a stem(chart) with a number
> (any number) to create the field I want to remove. For example, if I
> need to remove DeltaT.chart1, deltaT.chart6, deltaT.chart7, then my cell
> array would be:
> chart1
> chart6
> chart7
>
> The problem I have is when I get to chart 10 or more. If I have a
> number greater than 10 I would have the following:
> chart1
> chart6
> chart7
> chart10
> and I get the error ??? Error using ==> vertcat
> CAT arguments dimensions are not consistent.
>
> Error in ==> remover at 28
> toremove = [toremove;field]



 >> A=['chart1';'chart6';'chart7'];
 >> whos A

   Name Size Bytes Class Attributes

   A 3x6 36 char



> I think because I am trying to fit a 7 character wide (chart10) string
> into a 6 character wide cell array.

This isn't cell array!

> Is there a way around this or something I can do instead? My code is
> below if it helps.
>
>
> for a = 17%1:22
> listremove = []
> toremove = []

  toremove = {}

> rmvr = 0;
> Fileone = 'NODIPSinBTBTB_Delta';
> File = [Fileone,'Comp',int2str(a)]
> load([Fileone,int2str(a),'.mat'])
> data = deltaT;
> stem = 'chart';
> for L = 1:length(fieldnames(data))
> field = [stem,int2str(L)];
> if length(data.(field)) == 0
> rmvr = 1
> listremove = [listremove L];
> end
> end
> if rmvr == 1
> for g = 1:length(listremove)
> field = [stem,int2str(listremove(g))]
              toremove = [toremove;field]

     toremove = { toremove;field }

> end
> deltaT = rmfield(data, toremove)
> end
> save(File, 'deltaT')
> clear all
>
> end

 >> A={'chart1';'chart6';'chart7'; 'chart10'}

A =

     'chart1'
     'chart6'
     'chart7'
     'chart10'

 >> whos A
   Name Size Bytes Class Attributes

   A 4x1 498 cell


bartekltg

Subject: cell array dimensions do not match

From: Alex

Date: 13 Dec, 2012 01:34:10

Message: 3 of 4

Thanks, but that didnt quite work either. With those changes I got the error
??? Error using ==> rmfield at 21
FIELDNAMES must be a string or a cell array of strings.

Error in ==> remover at 33
        deltaT = rmfield(data, toremove)

I resolved it by modifying my code to remove the entires in the same for loop that combined the field numbers with the field name instead of trying to make a list.

    if rmvr == 1
        for g = listremove
            field = [stem,int2str(g)] % combining field numbers that contain no data with the stem they are named.
            data = rmfield(data, field) % removing the fields from data as their field names are made
        end
    end
    
    deltaT = data;
    
    save(File, 'deltaT')
    
    clear all

Alex

bartekltg <bartekltg@gmail.com> wrote in message <kab4mr$e40$1@node1.news.atman.pl>...
> W dniu 2012-12-13 00:21, Alex pisze:
> > I am having a problem where I am trying to create a cell array of
>
> So try cell array:)
>
> > strings to use in rmfield. I am combining a stem(chart) with a number
> > (any number) to create the field I want to remove. For example, if I
> > need to remove DeltaT.chart1, deltaT.chart6, deltaT.chart7, then my cell
> > array would be:
> > chart1
> > chart6
> > chart7
> >
> > The problem I have is when I get to chart 10 or more. If I have a
> > number greater than 10 I would have the following:
> > chart1
> > chart6
> > chart7
> > chart10
> > and I get the error ??? Error using ==> vertcat
> > CAT arguments dimensions are not consistent.
> >
> > Error in ==> remover at 28
> > toremove = [toremove;field]
>
>
>
> >> A=['chart1';'chart6';'chart7'];
> >> whos A
>
> Name Size Bytes Class Attributes
>
> A 3x6 36 char
>
>
>
> > I think because I am trying to fit a 7 character wide (chart10) string
> > into a 6 character wide cell array.
>
> This isn't cell array!
>
> > Is there a way around this or something I can do instead? My code is
> > below if it helps.
> >
> >
> > for a = 17%1:22
> > listremove = []
> > toremove = []
>
> toremove = {}
>
> > rmvr = 0;
> > Fileone = 'NODIPSinBTBTB_Delta';
> > File = [Fileone,'Comp',int2str(a)]
> > load([Fileone,int2str(a),'.mat'])
> > data = deltaT;
> > stem = 'chart';
> > for L = 1:length(fieldnames(data))
> > field = [stem,int2str(L)];
> > if length(data.(field)) == 0
> > rmvr = 1
> > listremove = [listremove L];
> > end
> > end
> > if rmvr == 1
> > for g = 1:length(listremove)
> > field = [stem,int2str(listremove(g))]
> toremove = [toremove;field]
>
> toremove = { toremove;field }
>
> > end
> > deltaT = rmfield(data, toremove)
> > end
> > save(File, 'deltaT')
> > clear all
> >
> > end
>
> >> A={'chart1';'chart6';'chart7'; 'chart10'}
>
> A =
>
> 'chart1'
> 'chart6'
> 'chart7'
> 'chart10'
>
> >> whos A
> Name Size Bytes Class Attributes
>
> A 4x1 498 cell
>
>
> bartekltg
>
>

Subject: cell array dimensions do not match

From: bartekltg

Date: 13 Dec, 2012 03:42:32

Message: 4 of 4

W dniu 2012-12-13 02:34, Alex pisze:
> Thanks, but that didnt quite work either. With those changes I got the
> error ??? Error using ==> rmfield at 21
> FIELDNAMES must be a string or a cell array of strings.
>
> Error in ==> remover at 33
> deltaT = rmfield(data, toremove)


Hmm, it works in my minimal example:

for j=1:5,
data(j).a1=rand(1);
data(j).a2=-rand(2);
data(j).a7=rand(2);
data(j).a11=rand(12);
end
toremove = {'a2';'a7';'a11'};
data2=rmfield(data,toremove);
disp(data2)


I also recreated (tip for next time. Try to create minimal
code, witch cause problem. I don't have your files:)


    listremove = []
    toremove = {}


for j=1:5,
data(j).a1=rand(1);
data(j).a2=-rand(2);
     data(j).a3=-rand(2);
     data(j).a4=-rand(2);
     data(j).a5=rand(3);
data(j).a7=rand(2);
data(j).a6=-rand(2);
end


    stem = 'a';
       for L = 1:6
        field = [stem,int2str(L)];
               if L>2
                  rmvr = 1
                  listremove = [listremove L];
               end
        end
    if rmvr == 1
        for g = 1:length(listremove)
            field = [stem,int2str(listremove(g))]
            toremove = [toremove;field]
        end
        deltaT = rmfield(data, toremove)
    end

It works.


BTW, if you always have structures with these same fields,
and you need faster code, listen to TideMan and use stucture
of vectors.

http://stackoverflow.com/questions/7274268/which-is-faster-vector-of-structs-or-a-number-of-vectors

c++, but some information apply to matlab.

bartekltg

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