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:
Invalid field name

Subject: Invalid field name

From: neil

Date: 17 May, 2010 08:03:05

Message: 1 of 4

I am currently trying to build a structure array about a list of student names which i import from excel. The list which i import is a two column array; column 1 is students name, column 2 is their grade. I am trying to set up a system so that i can call up their grades when their name appears in any subsequent array. So for example i would like to have:

I managed to get the following script to work for names like 'Bob' and 'Steve' but i've hit a major roadblock when it comes to character spaces between first and second names such as 'John Doe'.

%%%
s(length(textdata)).student=[textdata]

C=char(textdata)
x=1


while x<=length(textdata)
    
s(x).student=C(x,:)
s(x).(deblank(C(x,:)))=data(x)

x=x+1

end

fn = fieldnames(s)
C=char(deblank(textdata))

out1 =[];
x=1

while x<=length(textdata)
    
    newrow=[s.(deblank(C(x,:)))]
    out1=[out1;newrow]
    
    x=x+1
end


out1

data(:,2)=(out1)
%%%%

Any ideas how to overcome this "invalid field name" issue without having to trawl through the datasets manually pulling out character spaces??

Subject: Invalid field name

From: Sean

Date: 17 May, 2010 12:11:04

Message: 2 of 4

"neil " <neilfleming84@hotmail.com> wrote in message <hsqt7p$ftu$1@fred.mathworks.com>...
> I am currently trying to build a structure array about a list of student names which i import from excel. The list which i import is a two column array; column 1 is students name, column 2 is their grade. I am trying to set up a system so that i can call up their grades when their name appears in any subsequent array. So for example i would like to have:
>
> I managed to get the following script to work for names like 'Bob' and 'Steve' but i've hit a major roadblock when it comes to character spaces between first and second names such as 'John Doe'.
>
> %%%
> s(length(textdata)).student=[textdata]
>
> C=char(textdata)
> x=1
>
>
> while x<=length(textdata)
>
> s(x).student=C(x,:)
> s(x).(deblank(C(x,:)))=data(x)
>
> x=x+1
>
> end
>
> fn = fieldnames(s)
> C=char(deblank(textdata))
>
> out1 =[];
> x=1
>
> while x<=length(textdata)
>
> newrow=[s.(deblank(C(x,:)))]
> out1=[out1;newrow]
>
> x=x+1
> end
>
>
> out1
>
> data(:,2)=(out1)
> %%%%
>
> Any ideas how to overcome this "invalid field name" issue without having to trawl through the datasets manually pulling out character spaces??

a Hint:
>>name(isspace(name)) = [];

Subject: Invalid field name

From: neil

Date: 17 May, 2010 12:41:07

Message: 3 of 4

Cheers Sean! Managed to solve that problem.

Subject: Invalid field name

From: Steven Lord

Date: 17 May, 2010 13:44:07

Message: 4 of 4


"neil " <neilfleming84@hotmail.com> wrote in message
news:hsqt7p$ftu$1@fred.mathworks.com...
>I am currently trying to build a structure array about a list of student
>names which i import from excel. The list which i import is a two column
>array; column 1 is students name, column 2 is their grade. I am trying to
>set up a system so that i can call up their grades when their name appears
>in any subsequent array. So for example i would like to have:
>
> I managed to get the following script to work for names like 'Bob' and
> 'Steve' but i've hit a major roadblock when it comes to character spaces
> between first and second names such as 'John Doe'.

You could use ISSPACE to delete the space, as Sean suggested, but then you'd
run into problems for names like "John O'Hurley". If you need to consider
that case, look at GENVARNAME. Despite its name, it can be used to generate
struct array fieldnames, which have the same restrictions as variable names.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

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