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:
Run lines in a script

Subject: Run lines in a script

From: Chris McCorquodale

Date: 16 May, 2011 12:01:04

Message: 1 of 16

Hi,

Can anybody tell me if there is a way to tell matlab that you only want certain lines in a script to run?

For example:

run(script);

will run the whole script. But what if I only want it to run from line 10 or just run lines 20-30 say?

Thanks in advance,
Chris

Subject: Run lines in a script

From: ImageAnalyst

Date: 16 May, 2011 12:20:29

Message: 2 of 16

Chris:
Open the script in the editor. Then you can run as many files as you
want with the F10 key.

Or else you'd have to have logical variables to specify which chunks
of code you want to run or not run.

You can run some arbitrary number of lines from your script, like

numberOfLinesToRun = 42;
runMyScript(myScriptFileName, numberOfLinesToRun);

if you write runMyScript() properly, but no one will recommend that,
and probably won't even show you how to do it because using eval() is
generally not recommended in most situations.

Subject: Run lines in a script

From: Andy

Date: 16 May, 2011 14:16:02

Message: 3 of 16

"Chris McCorquodale" wrote in message <iqr3m0$hr0$1@newscl01ah.mathworks.com>...
> Hi,
>
> Can anybody tell me if there is a way to tell matlab that you only want certain lines in a script to run?
>
> For example:
>
> run(script);
>
> will run the whole script. But what if I only want it to run from line 10 or just run lines 20-30 say?
>
> Thanks in advance,
> Chris

This sounds like the perfect situation to take those 10 lines that you want to run repeatedly and separately from the rest of the script, and separate them from the rest of the script. Perhaps put those lines in a separate script? Or even better, perhaps you could turn those 10 lines into a function that might be useful elsewhere?

Subject: Run lines in a script

From: Chris McCorquodale

Date: 16 May, 2011 14:38:05

Message: 4 of 16

> This sounds like the perfect situation to take those 10 lines that you want to run repeatedly and separately from the rest of the script, and separate them from the rest of the script. Perhaps put those lines in a separate script? Or even better, perhaps you could turn those 10 lines into a function that might be useful elsewhere?

Thanks guys. These are the first things that come in to my head and would of course work. However, the application really dictates that its not that straight forward. I'm guessing that there is no easy way of doing this though and will just have to find a way around it!

Regards,

Chris

Subject: Run lines in a script

From: Andy

Date: 16 May, 2011 14:54:05

Message: 5 of 16

"Chris McCorquodale" wrote in message <iqrcsd$k9d$1@newscl01ah.mathworks.com>...
> > This sounds like the perfect situation to take those 10 lines that you want to run repeatedly and separately from the rest of the script, and separate them from the rest of the script. Perhaps put those lines in a separate script? Or even better, perhaps you could turn those 10 lines into a function that might be useful elsewhere?
>
> Thanks guys. These are the first things that come in to my head and would of course work. However, the application really dictates that its not that straight forward. I'm guessing that there is no easy way of doing this though and will just have to find a way around it!
>
> Regards,
>
> Chris

Could you perhaps describe what makes it "not that straight forward"?

Subject: Run lines in a script

From: Chris McCorquodale

Date: 16 May, 2011 15:10:23

Message: 6 of 16

> Could you perhaps describe what makes it "not that straight forward"?

I am using it with FEA program comsol. And basically the script defines a material, so in the script there are lines that matlab doesnt understand on its own, followed by a list of properties (q=2,w=3, etc - which matlab does understand) these are then used to do calculations on the stress analysis from comsol.

It is these properties at the end of the script, which I want to re-run to plot a graph (which will compare materials).

As you say, it can be done by separating the two, but I would rather keep them together for ease of running.

Thanks,

Chris

Subject: Run lines in a script

From: ImageAnalyst

Date: 16 May, 2011 15:39:55

Message: 7 of 16

Chris:
If you think it makes sense in your case you could do something like
this

function runMyScript(myScriptFileName, startLine, endLine)

fid = fopen(myScriptFileName, 'r');

% Read and throw away starting lines.
tline = fgetl(fid);
linesRead = 1;
while ischar(tline) && linesRead <startLine-1
    fprintf('Skipping %s\n', tline)
    tline = fgetl(fid);
    linesRead = linesRead + 1;
end

%Execute the lines we want
for k = startLine : endLine
    fprintf('Running %s\n', tline)
    tline = fgetl(fid);
    eval(tline); % Have MATLAB execute the line.
    if ~ischar(tline)
        break;
    end
end
fclose(fid);

Of course you'll need to check that the file exists, use try/catch,
and other normal error checking but that would be the gist of it.

Subject: Run lines in a script

From: Andy

Date: 16 May, 2011 15:52:05

Message: 8 of 16

"Chris McCorquodale" wrote in message <iqreov$qur$1@newscl01ah.mathworks.com>...
> > Could you perhaps describe what makes it "not that straight forward"?
>
> I am using it with FEA program comsol. And basically the script defines a material, so in the script there are lines that matlab doesnt understand on its own, followed by a list of properties (q=2,w=3, etc - which matlab does understand) these are then used to do calculations on the stress analysis from comsol.
>
> It is these properties at the end of the script, which I want to re-run to plot a graph (which will compare materials).
>
> As you say, it can be done by separating the two, but I would rather keep them together for ease of running.
>
> Thanks,
>
> Chris

This sounds like the perfect use of a function. You should pass the material and properties q, w, etc. into the function as arguments, and return whatever results come from the stress analysis. If you want to go even further, you could create a class for your material with properties q, w, etc. and a method for stress analysis. In fact, I vaguely remember a webinar doing something like this.

Subject: Run lines in a script

From: ImageAnalyst

Date: 16 May, 2011 16:34:11

Message: 9 of 16

On May 16, 11:52 am, "Andy " <myfakeemailaddr...@gmail.com> wrote:
> This sounds like the perfect use of a function.

---------------------------------------------------
But I think that he doesn't always know what lines he'll need to run -
they may vary each time. So he can't always just pull out the same
lines into a function. Recall he said "run from line 10 or just run
lines 20-30 say" - so I took this to mean that the starting and
stopping lines would vary and he'd need to be able to handle that. To
run a function, you'd have to have the same lines in the function all
the time.

Subject: Run lines in a script

From: dpb

Date: 16 May, 2011 16:41:45

Message: 10 of 16

On 5/16/2011 10:39 AM, ImageAnalyst wrote:
> Chris:
> If you think it makes sense in your case you could do something like
> this
>
> function runMyScript(myScriptFileName, startLine, endLine)
>
> fid = fopen(myScriptFileName, 'r');
>
> % Read and throw away starting lines.
> tline = fgetl(fid);
...[nice template example of picking and evaluating lines elided solely
for brevity]...

I'd wonder instead if it wouldn't be place for a routine that built the
script dynamically for submission--or rather than using eval how about a
script that simply copies the pertinent lines to a temporary function or
alternatively that comments out the non-Matlab'y lines before submitting
to execution?

Same idea but avoids eval

--

Subject: Run lines in a script

From: Andy

Date: 16 May, 2011 16:59:04

Message: 11 of 16

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <9dab40ab-a0d4-46a7-bd6e-4f434d07314f@v8g2000yqb.googlegroups.com>...
> On May 16, 11:52 am, "Andy " <myfakeemailaddr...@gmail.com> wrote:
> > This sounds like the perfect use of a function.
>
> ---------------------------------------------------
> But I think that he doesn't always know what lines he'll need to run -
> they may vary each time. So he can't always just pull out the same
> lines into a function. Recall he said "run from line 10 or just run
> lines 20-30 say" - so I took this to mean that the starting and
> stopping lines would vary and he'd need to be able to handle that. To
> run a function, you'd have to have the same lines in the function all
> the time.

He said "run from line 10 or just run lines 20-30" when he was initially directly asking how to run parts of the code. I thought this was a clear case of not describing "what he's really trying to do", but Chris can correct me if I'm wrong. From the later description of these lines as doing stress analysis on a material which is described by a set list of properties that sounds a lot like a function to me. Solutions like building a script dynamically or reading in the script and using eval(!) for the lines we want to keep, passing the line numbers as arguments (rather than simply the characteristics of the material) sound awfully roundabout and hack-ish. I would be very surprised if there is really an inherent need to execute particular lines of a script without the use of a function.

Chris, if you post the relevant lines of code, perhaps we could help you turn it into a function if it is not clear to you how to do so.

Subject: Run lines in a script

From: Chris McCorquodale

Date: 17 May, 2011 08:39:03

Message: 12 of 16

> He said "run from line 10 or just run lines 20-30" when he was initially directly asking how to run parts of the code. I thought this was a clear case of not describing "what he's really trying to do", but Chris can correct me if I'm wrong. From the later description of these lines as doing stress analysis on a material which is described by a set list of properties that sounds a lot like a function to me. Solutions like building a script dynamically or reading in the script and using eval(!) for the lines we want to keep, passing the line numbers as arguments (rather than simply the characteristics of the material) sound awfully roundabout and hack-ish. I would be very surprised if there is really an inherent need to execute particular lines of a script without the use of a function.
>
> Chris, if you post the relevant lines of code, perhaps we could help you turn it into a function if it is not clear to you how to do so.

I would appreciate that as I don't quite think i'm with you. Here is the code:

model.material.create('mat1');
model.material('mat1').name('Copper');
model.material('mat1').materialModel('def').set('relpermeability', '1');
model.material('mat1').materialModel('def').set('electricconductivity', '5.998e7[S/m]');
model.material('mat1').materialModel('def').set('thermalexpansioncoefficient', '17e-6[1/K]');
model.material('mat1').materialModel('def').set('heatcapacity', '385[J/(kg*K)]');
model.material('mat1').materialModel('def').set('relpermittivity', '1');
model.material('mat1').materialModel('def').set('density', '8700[kg/m^3]');
model.material('mat1').materialModel('def').set('thermalconductivity', '400[W/(m*K)]');
model.material('mat1').materialModel.create('Enu', 'Young''s modulus and Poisson''s ratio');
model.material('mat1').materialModel('Enu').set('poissonsratio', '0.31');
model.material('mat1').materialModel('Enu').set('youngsmodulus', '117[GPa]');
model.material('mat1').materialModel.create('linzRes', 'Linearized resistivity');
model.material('mat1').materialModel('linzRes').set('alpha', '0.0039[1/K]');
model.material('mat1').materialModel('linzRes').set('rho0', '1.72e-8[ohm*m]');
model.material('mat1').materialModel('linzRes').set('Tref', '298[K]');

UTS=256;
handles.UTS=UTS;
E=117000;
handles.E=E;
sigmaf=1000;
handles.sigmaf=sigmaf;
epsf=2.9;
handles.epsf=epsf;

As you can see matlab wont understand the first bit. As for the second, I have those handles defined as it is being used in a GUI.

Chris

Subject: Run lines in a script

From: ImageAnalyst

Date: 17 May, 2011 10:13:17

Message: 13 of 16

Chris:
Yes, but you didn't answer the main question, and that is "Are those
lines at the same line numbers for every single one of those files you
want to run, or do they occur at possibly different line numbers??

Subject: Run lines in a script

From: Chris McCorquodale

Date: 17 May, 2011 10:57:02

Message: 14 of 16

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <dc2a5dc5-0206-459f-8895-5d2d8e3c3b9e@w10g2000yqa.googlegroups.com>...
> Chris:
> Yes, but you didn't answer the main question, and that is "Are those
> lines at the same line numbers for every single one of those files you
> want to run, or do they occur at possibly different line numbers??

In general, it would be the same lines every time.

Subject: Run lines in a script

From: ImageAnalyst

Date: 17 May, 2011 12:03:00

Message: 15 of 16

On May 17, 6:57 am, "Chris McCorquodale" <c.mccorquod...@axon-
cable.co.uk> wrote:
> In general, it would be the same lines every time.
-----------------------------------------------
Then just extract those out into their own separate m-file.

Subject: Run lines in a script

From: Andy

Date: 17 May, 2011 12:03:02

Message: 16 of 16

"Chris McCorquodale" wrote in message <iqtk9u$1gj$1@newscl01ah.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <dc2a5dc5-0206-459f-8895-5d2d8e3c3b9e@w10g2000yqa.googlegroups.com>...
> > Chris:
> > Yes, but you didn't answer the main question, and that is "Are those
> > lines at the same line numbers for every single one of those files you
> > want to run, or do they occur at possibly different line numbers??
>
> In general, it would be the same lines every time.

I will preface this by saying that I don't know anything about FEA or comsol. I'm just sort of guessing based on the structure of the code. I take it that, in a line like:

model.material('mat1').materialModel('Enu').set('youngsmodulus', '117[GPa]');

it is the '117[GPa]' that might change depending on the material being tested? The list of parameters being set here is:

Name
'relpermeability'
'electricconductivity'
'thermalexpansioncoefficient'
'heatcapacity'
'relpermittivity'
'density'
'thermalconductivity'
'Enu'
'poissonsratio'
'youngsmodulus'
'linzRes'
'alpha'
'rho0'
'Tref'

Now imagine you had a function

function model = myFun(settings)
  % ...
end

where "settings" is a structure whose fieldnames are name, relpermeability, electricconductivity, and so on, and "model" is the returned model. Your script, instead of creating the model directly, would set up the "settings" structure and call the function to create the model. Then you could adjust the settings structure and call the function again. You could even do this last part in a loop. (That is, slightly adjust one parameter in a loop, recreating the model each time.)

If I'm misunderstanding the code, perhaps you could give a short explanation of it? It looks to me like model (or perhaps model.material?) is a class with these various properties and the script is just filling in the properties.

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