Thread Subject: Question on IF statements, I think...

Subject: Question on IF statements, I think...

From: jeremy

Date: 23 Jul, 2008 18:50:04

Message: 1 of 2

I've got the following code which is doing almost just what
I want it to do...

   load HC_actual;
   New_measurement = input('Enter new x/y data pair: ');
   HC_Chart_actual(end+1,:) = New_measurement;
   save HC_actual

Now for the question... How do I put the above code in my
M-file so that I can, from the command prompt, opt to enter
new data or just go on with the M-file without adding new
data to the HC_actual array?

Basically, I want a Command Window prompt that asks "Do you
want to enter new data?" and depending on whether I type in
'y' or 'n', the program either does the above code or skips
the code and continues the routine.

Any suggestions?

Thanks! Jeremy

Subject: Question on IF statements, I think...

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 23 Jul, 2008 19:01:05

Message: 2 of 2

In article <g67ugs$6bl$1@fred.mathworks.com>,
jeremy <rower15@excite.com> wrote:
>I've got the following code which is doing almost just what
>I want it to do...

> load HC_actual;

I would suggest that you get used to using the function form of load:
Matlab can do much better optimization if it doesn't have to deal with
variables in the workspace suddenly popping into existance.
You cannot (easily) use the command version of 'load' if you
are using nested functions for example.

> New_measurement = input('Enter new x/y data pair: ');
> HC_Chart_actual(end+1,:) = New_measurement;
> save HC_actual

>Now for the question... How do I put the above code in my
>M-file so that I can, from the command prompt, opt to enter
>new data or just go on with the M-file without adding new
>data to the HC_actual array?
>
>Basically, I want a Command Window prompt that asks "Do you
>want to enter new data?" and depending on whether I type in
>'y' or 'n', the program either does the above code or skips
>the code and continues the routine.


HC_struct = load('HC_actual.mat');
if ~isfield(HC_struct, 'HC_Chart_actual')
  error('Corrupt HC_actual file!');
end

addsomething = input('Do you want to enter new data? Y/[N]', 's');
if ~isempty(addsomething) && strcmpi(strtrim(addsomething),'y')
  New_measurement = input('Enter new x/y data pair: ');
  HC_struct.HC_Chart_actual(end+1,:) = New_measurement;
  save('HC_actual.mat', 'HC_struct', '-struct');
end

This code assumes that, either way, you will want HC_Chart_actual
in your workspace after the section of code. If you don't need
HC_Chart_actual unless the user wants to update it, then
you can move the load() call and associated sanity check
inside of the if statement.
--
  "Ignorance has been our king... he sits unchallenged on the throne of
  Man. His dynasty is age-old. His right to rule is now considered
  legitimate. Past sages have affirmed it. They did nothing to unseat
  him." -- Walter M Miller, Jr

Tags for this Thread

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com