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:
MATLAB programming script

Subject: MATLAB programming script

From: Chris McCorquodale

Date: 4 Jan, 2011 12:18:04

Message: 1 of 9

Hi,

I am doing analysis on a component that can be made of different materials and I have a script that works with FEA package comsol when a material is defined in comsol. However, what I want to do is define the material in the matlab interface (by means of m-file) and then run everything from matlab.

I have an m-file called material_copper which has all the relevant properties in it. The script works when m-file material_copper is embedded in it, but in the matlab interface I want to define the material as material_copper and have simply "material" written in the script at the appropriate part (this way i can define material as material_copper or material_steel etc etc). But when I write material=material_copper in the matlab interface, I get the following error...

??? Attempt to execute SCRIPT material_copper as a function:
C:\COMSOL35a\material_copper.m

Can anyone help here?

Thanks in advance,

Chris

Subject: MATLAB programming script

From: Husam Aldahiyat

Date: 4 Jan, 2011 13:01:20

Message: 2 of 9

"Chris McCorquodale" wrote in message <ifv35s$497$1@fred.mathworks.com>...
> Hi,
>
> I am doing analysis on a component that can be made of different materials and I have a script that works with FEA package comsol when a material is defined in comsol. However, what I want to do is define the material in the matlab interface (by means of m-file) and then run everything from matlab.
>
> I have an m-file called material_copper which has all the relevant properties in it. The script works when m-file material_copper is embedded in it, but in the matlab interface I want to define the material as material_copper and have simply "material" written in the script at the appropriate part (this way i can define material as material_copper or material_steel etc etc). But when I write material=material_copper in the matlab interface, I get the following error...
>
> ??? Attempt to execute SCRIPT material_copper as a function:
> C:\COMSOL35a\material_copper.m
>
> Can anyone help here?
>
> Thanks in advance,
>
> Chris

You can't do it in the interface, you'll need to rename the file.

Subject: MATLAB programming script

From: Chris McCorquodale

Date: 4 Jan, 2011 14:06:05

Message: 3 of 9

"Husam Aldahiyat" wrote in message <ifv5n0$hso$1@fred.mathworks.com>...
> "Chris McCorquodale" wrote in message <ifv35s$497$1@fred.mathworks.com>...
> > Hi,
> >
> > I am doing analysis on a component that can be made of different materials and I have a script that works with FEA package comsol when a material is defined in comsol. However, what I want to do is define the material in the matlab interface (by means of m-file) and then run everything from matlab.
> >
> > I have an m-file called material_copper which has all the relevant properties in it. The script works when m-file material_copper is embedded in it, but in the matlab interface I want to define the material as material_copper and have simply "material" written in the script at the appropriate part (this way i can define material as material_copper or material_steel etc etc). But when I write material=material_copper in the matlab interface, I get the following error...
> >
> > ??? Attempt to execute SCRIPT material_copper as a function:
> > C:\COMSOL35a\material_copper.m
> >
> > Can anyone help here?
> >
> > Thanks in advance,
> >
> > Chris
>
> You can't do it in the interface, you'll need to rename the file.

Hi Husam, thanks for your answer. Im a bit lost though, can you expand? How cant it be done in the interface? What will renaming the file do?

I mite have complicated things by going into too much detail. Basically all I want is my long script to have a prompt in it (e.g. material) and when it reaches this line in the script it executes the m-file that I assign that prompt to.

So if material=material_copper , then it will exectute the material_copper m-file when it reads "material" in the long script.

Thanks,
Chris

Subject: MATLAB programming script

From: Husam Aldahiyat

Date: 4 Jan, 2011 14:36:04

Message: 4 of 9

Rename the m-file to 'material'.

Then in your other script, run 'material' and the script will be executed.

Am I understanding this right?

Subject: MATLAB programming script

From: Chris McCorquodale

Date: 4 Jan, 2011 15:02:05

Message: 5 of 9

"Husam Aldahiyat" wrote in message <ifvb8k$ncp$1@fred.mathworks.com>...
> Rename the m-file to 'material'.
>
> Then in your other script, run 'material' and the script will be executed.
>
> Am I understanding this right?

I dont think you are.

Basically in the interface I want to define a command (such as material) as an m-file (in this case material_copper, in another case material_steel). In the long script, this command will be embedded and when matlab reads this command it will execute the m-file that the command is defined as.

So the material is a variable if you like and I want to be able to run the long script for any material I have stored as an m-file (material_steel, material_wood, material_iron, list goes on...etc).

Its so that into matlab I can type...

>> material = material_copper
>> run_long_script

to do an analysis for copper and...

>> material = material_steel
>> run_long_script

to do an analysis for steel. Does this make more sense?

Subject: MATLAB programming script

From: Husam Aldahiyat

Date: 4 Jan, 2011 15:25:20

Message: 6 of 9

"Chris McCorquodale" wrote in message <ifvcpd$65q$1@fred.mathworks.com>...
> "Husam Aldahiyat" wrote in message <ifvb8k$ncp$1@fred.mathworks.com>...
> > Rename the m-file to 'material'.
> >
> > Then in your other script, run 'material' and the script will be executed.
> >
> > Am I understanding this right?
>
> I dont think you are.
>
> Basically in the interface I want to define a command (such as material) as an m-file (in this case material_copper, in another case material_steel). In the long script, this command will be embedded and when matlab reads this command it will execute the m-file that the command is defined as.
>
> So the material is a variable if you like and I want to be able to run the long script for any material I have stored as an m-file (material_steel, material_wood, material_iron, list goes on...etc).
>
> Its so that into matlab I can type...
>
> >> material = material_copper
> >> run_long_script
>
> to do an analysis for copper and...
>
> >> material = material_steel
> >> run_long_script
>
> to do an analysis for steel. Does this make more sense?

I think I got it. Try this:

>> material = 'material_steel';
>> eval(material)

Some more tests:

>> arr = {'copper','steel','wood'};
>> eval(['material_',arr{2}])

Did it work like you wanted?

Subject: MATLAB programming script

From: Chris McCorquodale

Date: 4 Jan, 2011 15:42:05

Message: 7 of 9

> I think I got it. Try this:
>
> >> material = 'material_steel';
> >> eval(material)
>
> Some more tests:
>
> >> arr = {'copper','steel','wood'};
> >> eval(['material_',arr{2}])
>
> Did it work like you wanted?


Yes it did! Thank you very much!!!

Subject: MATLAB programming script

From: Walter Roberson

Date: 4 Jan, 2011 17:21:52

Message: 8 of 9

On 04/01/11 9:02 AM, Chris McCorquodale wrote:

> Basically in the interface I want to define a command (such as material)
> as an m-file (in this case material_copper, in another case
> material_steel). In the long script, this command will be embedded and
> when matlab reads this command it will execute the m-file that the
> command is defined as.

Instead of the eval() that was suggested, the appropriate command for
this is run()

run(material)

Subject: MATLAB programming script

From: Chris McCorquodale

Date: 5 Jan, 2011 08:06:05

Message: 9 of 9

Walter Roberson <roberson@hushmail.com> wrote in message <QGIUo.1697$rG3.343@newsfe09.iad>...
> Instead of the eval() that was suggested, the appropriate command for
> this is run()
>
> run(material)

Ok, thanks. Ill try this too. The eval() function still seemed to work though.

Tags for this Thread

No tags are associated with 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