Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to edit a Program in Matlab??

Status
Not open for further replies.

rahul.6sept

Full Member level 5
Joined
Nov 17, 2006
Messages
243
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Location
Guwahati, India
Activity points
2,889
Dear all,

Being a first time user, i'm facing problem in Matlab as how to edit a program in matlab.Suppose i've written a program of 200 lines in matlab.Now there exists some syntax error in line 10.
How should i edit the same??


Regards,

Rahul
 

Dear devilwar,

kindly tell me the procedure..rest i will try to do it by myself as no such program i have yet tried.Still in learning phase.So i wanna try out by me as of now.

Regards,

Rahul Chakraborty
 

You can edit the program as you write a program in the editor window. When you run a program the error appears on the command window in matlab. Click on the command window and you can go to the line which has the error. In case of syntax error Matlab is very user friendly as there appears a red line (same a it appears in word when you type a wrong spelling). When you take your mouse pointer to that line, there appears a "fix" to fix the error or there will be written what has to be done to rectify the error.
Orange color lines are just warning you can ignore.
 

Matlab opens with command window. For writing program you need to use Editor window.
 

Dear Devilwar,

If possible send me some basic matlab programs which might help me immensely in learning.

Regards,
Rahul Chakraborty
 

Dear Rahul,

When you open Matlab, the matlab command window appears. With command "edit" matlab editor window opens. This is just like a notepad window where you can write your program. See the program below

clc; %clears command window
clear all; %removes item for workspace and free system memory
close all; %close any open figures

t=-pi:0.01:pi;
ampl=5;
Sig_In1=ampl*sin(t);
Sig_In2=ampl*cos(t);
Sig_Out=Sig_In1+Sig_In2;

subplot(3,2,1)
plot(t,Sig_In1); grid on
xlabel('-\pi \leq \Theta \leq \pi')
ylabel('sin(\Theta)')
title('Plot of sin(\Theta)')

subplot(3,2,2)
plot(t,Sig_In2);grid on
xlabel('-\pi \leq \Theta \leq \pi')
ylabel('cos(\Theta)')
title('Plot of cos(\Theta)')

subplot(3,1,3)
plot(t,Sig_Out);grid on
xlabel('-\pi \leq \Theta \leq \pi')
ylabel('sin(\Theta)')
title('Plot of sin(\Theta)+cos(\Theta)')


This is a small program where a cos signal and a sin signal is added and result is output. You can follow some standard books for Matlab to start with. You can go for Getting started with matlab by rudra pratap.
 

Dear Devilwar,

Thanks for your reply.I have purchased the book by Rudra Pratap.Its indeed of great help for me. Actually i was writing codes in command window as a result i was unable to edit it.Now i learnt.

One thing i want to learn from you, user interactive program like that in C,can we do in Matlab, like for ex. checking Prime numbers in which user will pass on the number one by one to check whether the no. is Prime or unprime?If so how to write coding for user interactive program.Kindly one example for that.

But my ultimate aim is to learn how to solve differential equations of 2nd or higher orders (non-linear) which is something i actually need for my work.


Regards,

Rahul Chakraborty
 

you can use command 'input' for this.


close all;
clear all;
clc;

for p=1:4
num(p)=input('Enter a number: ');
fact = factor(num(p));
disp(['Factorial of ' num2str(num(p)) ' is: ']);
disp(fact);
end
This program displays factorial of a number which user inputs.
 

Dear Devilwar,

i tried this program but some syntax error appeared in disp(......) which i couldnot rectify it.


Rahul
 

I have checked the program its working fine in my PC. I would like to know the exact error thats poping in command window. I guess I might help you then.
 

Dear Devilwar,

when i write the line disp(['Factorial of 'num2str(num(p))' is :']; a red line comes under num2str(num(p)) and :'] and when mouse pointer brought over num2str it shows 'invalid syntax at num2str,possibly,a ),},or] is missing'.and when brought over :'] it shows 'Apparently a quoted string is terminated'.

When run the program it shows 'Unexpected Matlab expression'.


Regards,

Rahul Chakraborty
 

Hi Rahul

Code:
>>[B] disp(['Factorial of ''num2str(num(p))'' is :']);[/B]
Factorial of 'num2str(num(p))' is :


Blooz
 

when i write the line disp(['Factorial of 'num2str(num(p))' is :']; a red line comes under num2str(num(p)) and :']

Hey I see you have missed a ")" in the end of the line. It should have been as follows

disp(['Factorial of 'num2str(num(p))' is :']);
 

DEar Devilwar,

Thanks for your kind help.
One program that i tried y'day by myself is to plot xdot=sinx, with xdot along y axis and sinx along x axis but i'm getting error which is perhaps bcoz of error in logic hence want ur help in the same.

clc;
clear all;
function xdot = func(t,sinx);
xdot = sinx;
I saved it as func.m
in command prompt i typed

>>tspan = [0,2];x0=0;
>>[t,sinx]=ode23('func',tspan,x0);
here i'm getting error
Error: File:func.m Line:4 Column:1
Function definitions are not permitted at the prompt or in scripts.
Error in ==> odearguments at 81
if(nargin(ode)==2)
Error in ==>ode23 at 172
[neq,tspan,ntspan.....

One more thing i need to ask you is if it is necessary to have the function name and m file name to be the same as that is what i have seen in Rudra Pratap's book to be kept as same.

Regards,
Rahul
 

One more thing i need to ask you is if it is necessary to have the function name and m file name to be the same as that is what i have seen in Rudra Pratap's book to be kept as same.


Rahul

Yes the function name and .m file name should be same
 

you can change "function xdot=func(t,sinx);" become "function [xdot]=func(t,sinx);"
 

Thanks blooz & cokibolong,

But what in case of multifunction program?How can we have the m file name and function name as same in that case?

Regards,
Rahul
 

You can use Sub functions

A subfunction,visible only to the other functions in the same file, is created by defining a new function with the function keyword after the body of the preceding function or subfunction. Subfunctions are not visible outside the file where they are defined.

h**p://www.mathworks.in/help/techdoc/ref/function.html

---------- Post added at 17:27 ---------- Previous post was at 17:18 ----------

Eg

Here Inner_function is sub function

Code:
function x=major_function(y)
x=inner_function(y)+24;

function temp=inner_function(temp_y)
temp=temp_y+100;


Code:
>> major_function(1)

ans =

   125


---------- Post added at 17:38 ---------- Previous post was at 17:27 ----------

h**p://www.mathworks.in/help/techdoc/matlab_prog/f4-70666.html
 

hi cokibolong,

I tried what u suggested but it didn't work and the same error persists.Any other suggestion plz.

Regards,
rc
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top