Thread Subject: Import Excel Data into MATLAB Structure Variable

Subject: Import Excel Data into MATLAB Structure Variable

From: Peter Ma

Date: 20 Nov, 2009 15:27:03

Message: 1 of 6

Hi,

I have an Excel worksheet (extension *.xls) that contains data. The first row are column headers with text, and all following rows are numerical.

The column headers had such names as "P," "P_Sat," "T," "T_Sat," etc., so they were simple strings with no spaces or special characters.

I used MATLAB to import the Excel file and asked it to "Create vectors from each column using column names." I also checked the option to generate the m-function that could repeat the import action.

This worked flawlessly, and I got MATLAB variables containing an array of the data corresponding to the same column names and numerical data from the Excel worksheet.

Now, I want to assign these variables into structures by editing the auto-generated m-file from the import function. Using the same example column header names as listed above, I'd like it to produce, in the MATLAB workspace:

Import.Tank.Hydrogen.P
Import.Tank.Hydrogen.P_Sat
Import.Tank.Hydrogen.T
Import.Tank.Hydrogen.T_Sat
etc....

so that all the data are assigned into structures.

I tried doing that by editing the auto-generated import m-file by modifying the 2nd to last line:

FROM:
assignin('base', vars{i}, dataByColumn1.(vars{i}))

TO:
assignin('base', ['Import.Tank.Hydrogen.' vars{i}], dataByColumn1.(vars{i}))

But it gave me an error: "Invalid variable name "somevar.hoodle.hi" in ASSIGNIN."

Seeing that the command "ASSIGNIN" can't be used to assign values into structures, only arrays, is there an equivalent command for structures?

Subject: Import Excel Data into MATLAB Structure Variable

From: Peter Ma

Date: 20 Nov, 2009 15:44:08

Message: 2 of 6

>
> But it gave me an error: "Invalid variable name "somevar.hoodle.hi" in ASSIGNIN."
>

My apologies. The error message was actually supposed to be this:
"Invalid variable name "Import.Tank.Hydrogen.P" in ASSIGNIN."

Subject: Import Excel Data into MATLAB Structure Variable

From: jrenfree

Date: 20 Nov, 2009 17:59:10

Message: 3 of 6

On Nov 20, 7:44 am, "Peter Ma"
<Peter.H.MDELETE...@naDELETEMEsa.gDELETEMEov> wrote:
> > But it gave me an error: "Invalid variable name "somevar.hoodle.hi" in ASSIGNIN."
>
> My apologies. The error message was actually supposed to be this:
> "Invalid variable name "Import.Tank.Hydrogen.P" in ASSIGNIN."

What if you just assigned all the data into structures after you
already imported it in?

Subject: Import Excel Data into MATLAB Structure Variable

From: Peter Ma

Date: 20 Nov, 2009 19:13:20

Message: 4 of 6

jrenfree <jrenfree@gmail.com> wrote in message <76e4699c-2f95-4e5e-aacf-0a4f07e756ce@h14g2000pri.googlegroups.com>...
> On Nov 20, 7:44?am, "Peter Ma"
> <Peter.H.MDELETE...@naDELETEMEsa.gDELETEMEov> wrote:
> > > But it gave me an error: "Invalid variable name "somevar.hoodle.hi" in ASSIGNIN."
> >
> > My apologies. The error message was actually supposed to be this:
> > "Invalid variable name "Import.Tank.Hydrogen.P" in ASSIGNIN."
>
> What if you just assigned all the data into structures after you
> already imported it in?

How would you do this if you don't want to use the "eval" function? I want the last field in the structure path to be the same name as the imported column name. Also, the column (and subsequently the structure) names are different each time I call the function.

For example, for importing column "P_Sat," I'd like to make the structure "Import.Tank.P_Sat" on the fly, but if the column name is something else in the future, I'd like the function to be able to cerate the structure with a name of that "something else."

I was hoping ASSIGNIN would do the trick and be the better alternative to using EVAL to make dynamic variables, but is there no application for assigning structures?

Subject: Import Excel Data into MATLAB Structure Variable

From: Leslie McBrayer

Date: 20 Nov, 2009 19:35:18

Message: 5 of 6


> I used MATLAB to import the Excel file and asked it to "Create vectors
> from each column using column names." I also checked the option to
> generate the m-function that could repeat the import action.
>
> This worked flawlessly, and I got MATLAB variables containing an array of
> the data corresponding to the same column names and numerical data from
> the Excel worksheet.
>
> Now, I want to assign these variables into structures by editing the
> auto-generated m-file from the import function. Using the same example
> column header names as listed above, I'd like it to produce, in the MATLAB
> workspace:
>
> Import.Tank.Hydrogen.P
> Import.Tank.Hydrogen.P_Sat
> Import.Tank.Hydrogen.T
> Import.Tank.Hydrogen.T_Sat
> etc....
>
> so that all the data are assigned into structures.
>
> I tried doing that by editing the auto-generated import m-file by
> modifying the 2nd to last line:
>
> FROM:
> assignin('base', vars{i}, dataByColumn1.(vars{i}))
>
> TO:
> assignin('base', ['Import.Tank.Hydrogen.' vars{i}],
> dataByColumn1.(vars{i}))
>
> But it gave me an error: "Invalid variable name "somevar.hoodle.hi" in
> ASSIGNIN."
>
> Seeing that the command "ASSIGNIN" can't be used to assign values into
> structures, only arrays, is there an
equivalent command for structures?
>

For your purposes, I am not sure that you need the loop with the ASSIGNIN
statement at all. The code already creates a structure; the last FOR loop
breaks it apart.

Instead, try modifying the definition of the function so that it has an
output variable, such as:

% begin code
function outStruct = importfile1(fileToRead1)
% end code

and then replace the section beginning "% Create new variables..." with:

% begin code
outStruct = dataByColumn1; % or whatever struct is created by the
previous code
% end code

Then, you can call your function like this:

% begin code
Import.Tank.Hydrogen = importfile1('myHydrogenFile.xls')
%end

HTH,

Leslie

Subject: Import Excel Data into MATLAB Structure Variable

From: Peter Ma

Date: 23 Nov, 2009 14:38:19

Message: 6 of 6

"Leslie McBrayer" <lmcbrayer@mathworks.com> wrote in message <he6r1n$mbu$1@fred.mathworks.com>...
>
> For your purposes, I am not sure that you need the loop with the ASSIGNIN
> statement at all. The code already creates a structure; the last FOR loop
> breaks it apart.
>
> Instead, try modifying the definition of the function so that it has an
> output variable, such as:
>
> % begin code
> function outStruct = importfile1(fileToRead1)
> % end code
>
> and then replace the section beginning "% Create new variables..." with:
>
> % begin code
> outStruct = dataByColumn1; % or whatever struct is created by the
> previous code
> % end code
>
> Then, you can call your function like this:
>
> % begin code
> Import.Tank.Hydrogen = importfile1('myHydrogenFile.xls')
> %end
>
> HTH,
>
> Leslie
>

Leslie, that worked beautifully. Thank you!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
structure Peter M 20 Nov, 2009 10:29:12
variable Peter M 20 Nov, 2009 10:29:12
assignin Peter M 20 Nov, 2009 10:29:12
excel Peter M 20 Nov, 2009 10:29:12
import Peter M 20 Nov, 2009 10:29:12
mfile Peter M 20 Nov, 2009 10:29:12
mfunction Peter M 20 Nov, 2009 10:29:12
importdata Peter M 20 Nov, 2009 10:29:12
rssFeed for this Thread

Contact us at files@mathworks.com