Quick basic question on script files

9 views (last 30 days)
John connor
John connor on 18 Mar 2015
Commented: John connor on 18 Mar 2015
Hey fellas. I have an exam coming up for university. There's obviously going to be a bunch of set questions and we will need to carry them out on a script.
I wanted to ask, In the case where I have to assign X to a value for lets say, question 1 and then assign X to a different value for Question 2, how would we go about doing this? Would you assign X in the Command Window for both questions and when you run the script will it just take the X you assigned during the writing of the question?
I would think that if two different questions required me to assign X to two different values, when I clicked run, It would just treat X as the last value I entered?
Please advise.
Also,
We are told to use user comments. What would you recommend we put in user comments for basic questions? I.E.
Lets just say the question was something really simple like 1) Compute x+2 in Matlab assuming X = 2.
Would this suffice?
%Question 1
%Solving X + 2 when X = 2.
disp ( 'part a' )
>> X = 2;
>> 2 + X
We weren't exactly told how to make use of user comments. Just that we should use them.
I really hope this I could explain both questions properly. Thank you all for the help

Answers (1)

Andreas Goser
Andreas Goser on 18 Mar 2015
Use of variables: You are right, you would overwrite the value of the first result. I would probably uses a mixture of naming conventions, like X1, X2. If it is a larger set of questions, you may want to look into cell arrays.
Commenting: There is a thin line between unnecessary commenting the obvious and udeful commenting. It is also about why commenting in the first place. In real life, you want to comment that a successor or you in a couple of years understands the code. For an exam, the focus might be on the content of the exam, so if it is about signal processing, you should focus on that and not programming techniques.
  1 Comment
John connor
John connor on 18 Mar 2015
Thanks for the swift response and for covering basically everything I asked. You wrote down my exact thoughts. I was writing each question as X1,X2,X3, etc. I'm just a tad bit worried that I will lose marks because of this. I should probably try take it up with my unit coordinator. Either way, I don't think I should be losing marks for it.
Thanks again man

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!