MATLABMATLAB · Chapterبەش 5 · Lectureوانە 5.1

Script Files, input, disp and fprintfفایلەکانی دەسنووس، input، disp و fprintf

Turn typed commands into a program: write and run .m script files, name them well, manage files and folders from MATLAB, ask the user with input (and why it evaluates what is typed), and show results with disp, num2str and fprintf — conversion codes, width, precision and a format that repeats over a whole vector.

نووسراو بکە بە : ⁦.m⁩ بنووسە و جێبەجێیان بکە، ناوی باشیان لێ بنێ، لەناو MATLABـەوە و بەڕێوە ببە، بە input پرسیار لە بکە (و بۆچی ئەوەی نووسراوە هەژمار دەکات) و ئەنجامەکان بە disp، ⁦num2str⁩ و fprintf پیشان بدە — ، پانی، وردی و فۆرماتێک کە بۆ هەموو دووبارە دەبێتەوە.

23 min read24 خولەک خوێندنەوە · 20 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Create, save and run a script file, and explain why its variables stay in the shared Workspace.
  • Choose valid file names that do not hide built-in functions.
  • Use pwd, cd, dir, type, mkdir, copyfile, movefile, delete, save, load and diary.
  • Read numbers with input and text with input(prompt, 's'), and explain why input evaluates.
  • Show results with disp, num2str and fprintf, using conversion codes, width, precision and n.
  • دروست بکەیت، هەڵیبگریت و جێبەجێی بکەیت و ڕوونی بکەیتەوە بۆچی لە هاوبەشدا دەمێننەوە.
  • ناوی دروست بۆ هەڵبژێریت کە نەشارنەوە.
  • pwd، cd، dir، type، mkdir، copyfile، movefile، delete، save، load و diary بەکار بهێنیت.
  • ژمارە بە input و دەق بە ⁦input(prompt, 's')⁩ بخوێنیتەوە و ڕوونی بکەیتەوە بۆچی input هەژمار دەکات.
  • ئەنجامەکان بە disp، ⁦num2str⁩ و fprintf پیشان بدەیت، بە بەکارهێنانی ، پانی، وردی و n.

From commands to a script fileلە فەرمانەکانەوە بۆ فایلی دەسنووس

Until now you typed one command at a time at the >> prompt. That is fine for a quick sum, but the commands are gone when you close MATLAB, and to change one number you must type everything again. A solves this: it is a plain text file with the extension .m that holds a list of MATLAB commands. When you run it, MATLAB carries out the commands from top to bottom, exactly as if you had typed them one after another.

تا ئێستا لە دوای >> هەر جارێک دەنووسیت. ئەمە بۆ کۆکردنەوەیەکی خێرا باشە، بەڵام کاتێک MATLAB دادەخەیت نامێنن و بۆ گۆڕینی تەنها یەک ژمارە دەبێت هەموویان دووبارە بنووسیتەوە. ئەم کێشەیە چارەسەر دەکات: بە .m کە لیستێک لە MATLAB هەڵدەگرێت. کاتێک جێبەجێی دەکەیت، MATLAB لە سەرەوە بۆ خوارەوە ئەنجام دەدات، ڕێک وەک ئەوەی یەک لە دوای یەک بە خۆت نووسیبێتت.

Four steps to your first scriptچوار هەنگاو بۆ یەکەم دەسنووس
  1. Createدروستکردن

    On the Home tab click New Script, or type edit circle in the Command Window. The Editor opens (you met it in the desktop).

    لە تابی Home کلیک لە ⁦New Script⁩ بکە، یان لە ⁦Command Window⁩ بنووسە edit circle. Editor دەکرێتەوە (لە ڕووکاری MATLAB ناسیت).

  2. Writeنووسین

    Type the commands, one per line.

    بنووسە، هەر یەکەیان لە دێڕێکدا.

  3. Saveپاشەکەوتکردن

    Save with Ctrl+S. The file name becomes the script's name: circle.m.

    بە ⁦Ctrl+S⁩ هەڵیبگرە. ناوی دەبێتە ناوی : circle.m.

  4. Runجێبەجێکردن

    Type the name without .m at the prompt (circle) and press Enter, or press the Run button in the Editor (F5).

    ناوەکە بێ .m لە دوای بنووسە (circle) و Enter دابگرە، یان دوگمەی Run لە Editor دابگرە ⁦(F5)⁩.

A first script (the radius is fixed for now)یەکەم دەسنووس (بۆ ئێستا نرخی نیوەتیرەکە جێگیرە)circle.mMATLABType in a terminalلە تێرمیناڵدا بینووسە
% circle.m - area and circumference of a circle
r = 2.5;
area = pi*r^2
circ = 2*pi*r
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

A line: MATLAB skips it. It tells people what the file does.

دێڕی : MATLAB پشتگوێی دەخات. بە خەڵک دەڵێت چی دەکات.

Lineدێڕی 3-4

No semicolon, so MATLAB shows each result when the script runs.

نییە، بۆیە کاتێک جێبەجێ دەبێت MATLAB هەر ئەنجامێک پیشان دەدات.

Quick checkپشکنینی خێراYou saved the script room_area.m. How do you run it from the Command Window? room_area.mـت هەڵگرت. چۆن لە ⁦Command Window⁩ جێبەجێی دەکەیت؟

MATLAB Crash Course for BeginnersfreeCodeCamp.org · Englishئینگلیزی · 1.7M viewsبینین · 37K likesلایک · Partبەشی 10:04–13:24 (ofلە 1:57:45)

A MATLAB beginner course by Phil Parisi: the Command Window, variables, vectors and matrices, indexing, scripts, sections and naming rules, each in short parts.

کۆرسێکی سەرەتایی MATLAB لەلایەن ⁦Phil Parisi: Command Window⁩، ، و ، ، ، بەشەکان و یاساکانی ناونان، هەر یەکەیان بە بەشێکی کورت.

Watch 10:04-13:24: New Script, save the .m file, run it with Run or Ctrl+Enter, then add % comments on their own line or at the end of a line.

لە ⁦10:04⁩ تا ⁦13:24⁩ سەیر بکە: ⁦New Script⁩، .m، جێبەجێکردنی بە Run یان ⁦Ctrl+Enter⁩، پاشان نووسینی بە % لەسەر دێڕی خۆیان یان لە کۆتایی دێڕێکدا.

From commands to a script fileلە فەرمانەکانەوە بۆ فایلی دەسنووس

Scripts share the Workspaceدەسنووسەکان شوێنی کار بەشدار دەکەن

A script has no private memory. Every variable it creates lands in the same as your typed commands, and stays there after the script ends — MathWorks' page on scripts says the variables remain in the workspace. That is handy: after circle runs you can type area*2 at the prompt. Octave works the same way: its manual says a script's variables are in the same scope as the variables of the command line.

تایبەتی نییە. هەر کە دروستی دەکات دەچێتە هەمان ئەو تێیدان و دوای کۆتاییهاتنی لەوێ دەمێنێتەوە — پەڕەی MathWorks دەربارەی دەڵێت لە دەمێننەوە. ئەمە بەسوودە: دوای جێبەجێبوونی circle دەتوانیت area*2 لە دوای بنووسیت. Octaveـیش هەروایە: ڕێبەرەکەی دەڵێت لە هەمان بواری هێڵی فەرمانن.

Four parts of the MATLAB desktop. The Editor holds circle.m with five lines. Arrow 1, Save, goes down to the Files panel, which lists circle.m and lab1.mat: the file name is the script name. Arrow 2, Run, goes to the Command Window, where >> circle was typed, the prompt Radius in cm: got 2.5 and a line Area = 19.63 was printed. Arrow 3 goes down to the Workspace panel, which now holds area 19.6350, circ 15.7080 and r 2.5000: the variables stay in the shared workspace.چوار بەشی ڕووکاری MATLAB. Editor فایلی circle.m بە پێنج دێڕەوە هەڵدەگرێت. تیری یەکەم (پاشەکەوتکردن) بەرەو پانێڵی Files دەچێت کە circle.m و lab1.mat پیشان دەدات: ناوی فایل هەمان ناوی دەسنووسە. تیری دووەم (Run) بەرەو Command Window دەچێت، کە تێیدا circle نووسراوە، دەقی داواکردنەکە ژمارە 2.5ـی وەرگرتووە و دێڕی Area = 19.63 چاپ کراوە. تیری سێیەم بەرەو پانێڵی Workspace دەچێت کە ئێستا area، circ و r هەڵدەگرێت: گۆڕاوەکان لە شوێنی کار هاوبەشدا دەمێننەوە.
Where things live: the script file, the run, and the variables it leaves behindهەر شتێک لە کوێیە: ، جێبەجێکردن و ئەو بەجێی دەهێڵێتCêIT

The other side of sharing: a script can also use and overwrite variables that already exist. If your script uses r but you forget the line that sets it, MATLAB quietly uses whatever r is left in the workspace from earlier work — a wrong answer with no error. Two habits help: set every variable the script needs inside the script, and when in doubt clear the workspace first with clear (managing your session).

لایەنی تری بەشداربوون: دەتوانێت ئەو کە پێشتر هەن بەکار بهێنێت و بیانگۆڕێت. ئەگەر r بەکار بهێنێت بەڵام ئەو دێڕەت بیر بچێت کە دەداتێ، MATLAB بە بێدەنگی هەر rـێک بەکار دەهێنێت کە لە کاری پێشووەوە لە ماوەتەوە — وەڵامێکی هەڵە بێ هیچ . دوو ڕاهاتن یارمەتی دەدەن: هەموو ئەو پێویستی پێیانە لەناو خودی بدە و ئەگەر دڵنیا نیت، سەرەتا بە clear پاک بکەرەوە (بەڕێوەبردنی کارەکەت).

Quick checkپشکنینی خێراA script sets total = 50; and ends. What happens when you then type total + 1 at the prompt? total = 50; دادەنێت و کۆتایی دێت. ئەگەر پاشان total + 1 لە دوای بنووسیت چی ڕوودەدات؟

From commands to a script fileلە فەرمانەکانەوە بۆ فایلی دەسنووس

Choosing a good file nameهەڵبژاردنی ناوێکی باش بۆ فایل

You run a script by its file name, so the name must be a valid MATLAB name: the same rules as for variables. It starts with a letter, then uses only letters, digits and the _ — no spaces, no -, no dots except the one before m. MATLAB is : Circle.m and circle.m are different names.

بە ناوی جێبەجێ دەکەیت، بۆیە ناوەکە دەبێت ناوێکی دروستی MATLAB بێت: هەمان یاساکانی ناوی گۆڕاوەکان. بە پیت دەست پێدەکات، پاشان تەنها پیت، ژمارە و _ بەکار دەهێنێت — و - نا، خاڵیش نا جگە لەوەی پێش m. MATLAB : Circle.m و circle.m دوو ناوی جیاوازن.

File names for scriptsناوی بۆ
File nameناوی Can you run it?دەتوانیت جێبەجێی بکەیت؟Whyبۆچی
circle_area.myesبەڵێletters and _پیت و _
lab2_q1.myesبەڵێdigits are fine after the first letterژمارە دوای پیتی یەکەم ڕێگەپێدراوە
circle area.mnoنەخێرMATLAB reads circle and area.m as two wordsMATLAB circle و area.m وەک دوو وشە دەخوێنێتەوە
2nd_try.mnoنەخێرstarts with a digitبە ژمارە دەست پێدەکات
lab-2.mnoنەخێرlab-2 means lab minus 2lab-2 واتە lab کەم 2
sum.myes, but never do itبەڵێ، بەڵام هەرگیز مەیکەit hides MATLAB's own sum function sumـی خودی MATLAB دەشارێتەوە

Also avoid a name that is already used — by a built-in function (sum.m, input.m, disp.m) or by a variable in your workspace. If a variable called circle exists, typing circle shows the variable instead of running your script. exist circle tells you what a name already means (0 = unused, 1 = a variable, 2 = a file).

هەروەها خۆت لە ناوێک بپارێزە کە پێشتر بەکارهاتووە — لەلایەن (sum.m، input.m، disp.m) یان لە . ئەگەر بە ناوی circle هەبێت، نووسینی circle لە جیاتی جێبەجێکردنی پیشان دەدات. exist circle پێت دەڵێت ناوێک پێشتر چ واتایەکی هەیە (⁦0⁩ = بەکارنەهاتووە، 1 = ، 2 = ).

Quick checkپشکنینی خێراWhich file name can you run as a script?کام ناوی دەتوانیت وەک جێبەجێ بکەیت؟

MATLAB Crash Course for BeginnersfreeCodeCamp.org · Englishئینگلیزی · 1.7M viewsبینین · 37K likesلایک · Partبەشی 1:45:09–1:45:54 (ofلە 1:57:45)

A MATLAB beginner course by Phil Parisi: the Command Window, variables, vectors and matrices, indexing, scripts, sections and naming rules, each in short parts.

کۆرسێکی سەرەتایی MATLAB لەلایەن ⁦Phil Parisi: Command Window⁩، ، و ، ، ، بەشەکان و یاساکانی ناونان، هەر یەکەیان بە بەشێکی کورت.

Watch 1:45:09-1:45:54: a script needs the .m extension, and MATLAB refuses a file name that starts with a digit.

لە ⁦1:45:09⁩ تا ⁦1:45:54⁩ سەیر بکە: پێویستی بە .m هەیە و MATLAB ناوی کە بە دەست پێبکات قبووڵ ناکات.

From commands to a script fileلە فەرمانەکانەوە بۆ فایلی دەسنووس

Comments and sections in a scriptلێدوانەکان و بەشەکان لە دەسنووسدا

A % starts a (comments). In a script, comments are worth more than at the prompt: put a few comment lines at the very top that say what the script does. MATLAB then shows those lines as help: help circle prints them.

% دەست پێدەکات (لێدوانەکان). لە لە ⁦Command Window⁩ بەنرختترن: چەند دێڕێکی لە سەرەوەی دابنێ کە بڵێن چی دەکات. MATLAB ئەو دێڕانە وەک یارمەتی پیشان دەدات: help circle چاپیان دەکات.

A line that starts with two percent signs, %%, begins a section. In MATLAB's Editor you can run just the section where the cursor is (Run Section, Ctrl+Enter) — useful for a lab sheet with several questions in one file. In Octave a %% line is simply a comment, so the file still runs from top to bottom there.

دێڕێک کە بە دوو نیشانەی سەدی %% دەست پێبکات، بەشێک دەست پێدەکات. لە Editorـی MATLABـدا دەتوانیت تەنها ئەو بەشە جێبەجێ بکەیت کە تێدایە (⁦Run Section⁩، ⁦Ctrl+Enter⁩) — بۆ پەڕەیەکی تاقیگە کە چەند پرسیارێکی لە یەک هەیە بەسوودە. لە Octaveـدا دێڕی %% تەنها ، بۆیە لەوێ هەر لە سەرەوە بۆ خوارەوە جێبەجێ دەبێت.

Two questions in one file, as two sectionsدوو پرسیار لە یەک فایلدا، وەک دوو بەشlab_sheet.mMATLABType in a terminalلە تێرمیناڵدا بینووسە
%% Question 1: area of a rectangle
w = 4; len = 6;
area1 = w*len

%% Question 2: area of a circle
r = 3;
area2 = pi*r^2
Quick checkپشکنینی خێراWhat does a line starting with %% do in MATLAB's Editor?دێڕێک کە بە %% دەست پێدەکات لە Editorـی MATLABـدا چی دەکات؟

MATLAB Crash Course for BeginnersfreeCodeCamp.org · Englishئینگلیزی · 1.7M viewsبینین · 37K likesلایک · Partبەشی 1:26:01–1:28:19 (ofلە 1:57:45)

A MATLAB beginner course by Phil Parisi: the Command Window, variables, vectors and matrices, indexing, scripts, sections and naming rules, each in short parts.

کۆرسێکی سەرەتایی MATLAB لەلایەن ⁦Phil Parisi: Command Window⁩، ، و ، ، ، بەشەکان و یاساکانی ناونان، هەر یەکەیان بە بەشێکی کورت.

Watch 1:26:01-1:28:19: %% starts a section; Run Section (Ctrl+Enter) runs only the section with the cursor, Run runs the whole script.

لە ⁦1:26:01⁩ تا ⁦1:28:19⁩ سەیر بکە: %% بەشێک دەست پێدەکات؛ ⁦Run Section (Ctrl+Enter)⁩ تەنها ئەو بەشە جێبەجێ دەکات کە تێدایە، Run هەموو جێبەجێ دەکات.

Files and folders from MATLABفایلەکان و فۆڵدەرەکان لەناو MATLABـدا

MATLAB looks for your scripts in the current folder (the Files panel shows it) and then in a list of other folders called the search path. A few commands let you manage files without leaving the Command Window. They are the same ideas as files and folders in Windows and the command line:

MATLAB سەرەتا لە ئێستا (پانێڵی Files پیشانی دەدات) بەدوای دەگەڕێت، پاشان لە لیستێکی تری کە پێی دەڵێن ⁦search path⁩. چەند ڕێگەت پێدەدەن بێ ئەوەی ⁦Command Window⁩ جێبهێڵیت بەڕێوە ببەیت. هەمان بیرۆکەکانی فایلەکان و فۆڵدەرەکان لە Windowsـدا و هێڵی فەرمانن:

File and system commands from the course's commands list و سیستەم لە لیستی وانەکە
CommandWhat it doesچی دەکات
pwdShows the current folder (print working directory). ئێستا پیشان دەدات (⁦print working directory⁩).
cd labsChanges the current folder to labs; cd .. goes up one level. ئێستا دەکات بە labs؛ cd .. ئاستێک سەردەکەوێت.
dirLists the files and folders in the current folder (ls works too). و ئێستا لیست دەکات (lsـیش کار دەکات).
whatLists only the MATLAB files: .m and .mat.تەنها MATLAB لیست دەکات: .m و .mat.
type circle.mPrints the text of a file in the Command Window.دەقی لە ⁦Command Window⁩ چاپ دەکات.
mkdir labsMakes a new folder. نوێ دروست دەکات.
copyfile a.m b.mCopies a file. کۆپی دەکات.
movefile b.m oldMoves (or renames) a file. دەگوازێتەوە (یان ناوی دەگۆڕێت).
delete b.mDeletes a file. Be careful: check the name first. دەسڕێتەوە. ئاگاداربە: سەرەتا ناوەکە بپشکنە.
save lab1.mat x nameKeeps the variables x and name in a .mat file (all variables if you list none). x و name لە .matـدا هەڵدەگرێت (ئەگەر هیچ ناوێک نەنووسیت، هەموو ).
load lab1.matBrings the saved variables back into the workspace. دەگەڕێنێتەوە بۆ .
diary session.txtRecords everything you type and see into a text file, until diary off.هەموو ئەوەی دەینووسیت و دەیبینیت لە تۆمار دەکات، تا diary off.
pathLists the folders of the search path. ⁦search path⁩ لیست دەکات.
dateToday's date as text, for example '24-Oct-2026'.بەرواری ئەمڕۆ وەک دەق، بۆ نموونە '24-Oct-2026'.
Quick checkپشکنینی خێراYou are not sure which folder MATLAB is working in. Which command tells you?دڵنیا نیت MATLAB لە کام کار دەکات. کام پێت دەڵێت؟

Files and folders from MATLABفایلەکان و فۆڵدەرەکان لەناو MATLABـدا

Keeping variables: save and loadهەڵگرتنی گۆڕاوەکان: save و load

Two commands of the list keep your work between sessions: save writes variables into a .mat file and load brings them back into the workspace. A .mat file holds the variables themselves (names and values), not the commands that made them — that is what a script is for:

دوو لە لیستەکە کارەکەت لە نێوان دوو دانیشتندا دەپارێزن: save لە .matـدا دەنووسێت و load دەیانگەڕێنێتەوە بۆ . .mat خودی هەڵدەگرێت (ناو و )، نەک ئەو دروستیان کردوون — ئەوە کاری :

Keeping variables in a .mat fileهەڵگرتنی گۆڕاوەکان لە فایلێکی ⁦.mat⁩ـداMATLABType in a terminalلە تێرمیناڵدا بینووسە
x = 7; name = 'Hana';
save lab1.mat x name
clear
load lab1.mat
x
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Writes x and name into lab1.mat in the current folder.

x و name لە lab1.matـدا لە ئێستا دەنووسێت.

Lineدێڕی 3

The workspace is now empty.

ئێستا بەتاڵە.

Lineدێڕی 4-5

load brings both variables back, so x is 7 again.

load هەردوو دەگەڕێنێتەوە، بۆیە x دووبارە 7ـە.

Quick checkپشکنینی خێراYou want to keep the variables of today's lab and use them next week. Which command?دەتەوێت تاقیگەی ئەمڕۆ هەڵبگریت و هەفتەی داهاتوو بەکاریان بهێنیت. کام ؟

Asking the user: inputپرسیار لە بەکارهێنەر: input

A script with a fixed radius always gives the same answer. To let the person at the keyboard choose, use input. It prints a , waits, and gives back what was typed: r = input('Radius: ') shows Radius: , waits for you to type and press Enter, and stores the answer in r. Every lab program in this track starts this way.

بە نیوەتیرەیەکی جێگیر هەمیشە هەمان وەڵام دەدات. بۆ ئەوەی ئەو کەسەی بەکار دەهێنێت هەڵبژێرێت، input بەکار بهێنە. چاپ دەکات، چاوەڕێ دەکات و ئەوەی نووسراوە دەیگەڕێنێتەوە: r = input('Radius: ') نووسینی Radius: پیشان دەدات، چاوەڕێ دەکات تا بنووسیت و Enter دابگریت، پاشان وەڵامەکە لە rـدا هەڵدەگرێت. هەموو تاقیگە لەم بەشەدا بەم شێوەیە دەست پێدەکەن.

Here is the important part: input evaluates what you type, exactly as the >> prompt would. MathWorks' page says you can type expressions such as pi/4 or rand(3) and even use variables from the workspace. So 2.5, pi/4, 3*2 and [1 2 3] are all fine answers.

ئەمەش خاڵە گرنگەکەیە: input ئەوەی دەینووسیت وەک هەژمار دەکات، ڕێک وەک >>. پەڕەی MathWorks دەڵێت دەتوانیت وەک pi/4 یان rand(3) بنووسیت و تەنانەت ناو بەکار بهێنیت. بۆیە 2.5، pi/4، 3*2 و [1 2 3] هەموویان وەڵامی دروستن.

A typed sample session (what you type follows each prompt)دانیشتنێکی نموونە (ئەوەی دەینووسیت لە دوای هەر دەقێکی داواکردن دێت)MATLABType in a terminalلە تێرمیناڵدا بینووسە
>> r = input('Radius: ')
>> r = input('Radius: ')
>> name = input('Your name: ', 's')
>> x = input('Number: ')
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

Typed 2.5: a number.

نووسرا 2.5: ژمارەیەک.

Lineدێڕی 2

Typed pi/4: MATLAB works it out and stores 0.7854.

نووسرا pi/4: MATLAB هەژماری دەکات و ⁦0.7854⁩ هەڵدەگرێت.

Lineدێڕی 3

With 's' the answer is kept as text, exactly as typed.

بە 's' وەڵامەکە وەک دەق هەڵدەگیرێت، ڕێک وەک نووسراوە.

Lineدێڕی 4

Only Enter was pressed: input gives an empty array [].

تەنها Enter داگیرا: input بەتاڵ [] دەدات.

Quick checkپشکنینی خێراAt the prompt of x = input('Number: ') you type 10/4. What is stored in x?لە دوای x = input('Number: ') دەنووسیت 10/4. چی لە xـدا هەڵدەگیرێت؟

Asking the user: inputپرسیار لە بەکارهێنەر: input

Reading text: the 's' optionخوێندنەوەی دەق: هەڵبژاردەی 's'

What if you ask for a name and the user types Shilan? Because input evaluates, MATLAB looks for a variable or function called Shilan, finds none, shows an and asks again. For any answer that is text — a name, a city, a yes/no answer — add 's' as a second argument: name = input('Your name: ', 's'). Then MATLAB evaluates nothing and gives back the characters exactly as typed. If the user just presses Enter, the result is empty text ''.

ئەگەر ناو بپرسیت و بنووسێت Shilan چی دەبێت؟ لەبەر ئەوەی input هەژمار دەکات، MATLAB بەدوای یان بە ناوی Shilanدا دەگەڕێت، هیچ نادۆزێتەوە، پیشان دەدات و دووبارە دەپرسێتەوە. بۆ هەر وەڵامێک کە دەق بێت — ناو، شار، بەڵێ/نەخێر — 's' وەک دووەم زیاد بکە: name = input('Your name: ', 's'). ئەوکات MATLAB هیچ هەژمار ناکات و ڕێک وەک نووسراون دەگەڕێنێتەوە. ئەگەر تەنها Enter دابگرێت، ئەنجامەکە دەقێکی بەتاڵی ''ـە.

Quick checkپشکنینی خێراYou want to read a city name such as Duhok. Which line is right?دەتەوێت ناوی شارێکی وەک Duhok بخوێنیتەوە. کام دێڕ دروستە؟

Asking the user: inputپرسیار لە بەکارهێنەر: input

Why evaluating input is riskyبۆچی هەژمارکردنی نێوخستە مەترسیدارە

Evaluating is convenient in a lab — you can type pi/4 — but it also means the typed text runs as MATLAB code. Whoever sits at the keyboard can type any expression, including ones that call functions with side effects. Use plain input only in your own small programs. When other people type, read text with 's' and turn it into a number yourself with str2double, which gives NaN (not a number) when the text is not a number:

هەژمارکردن لە تاقیگەدا ئاسانکارییە — دەتوانیت pi/4 بنووسیت — بەڵام واتای ئەوەشە کە دەقی نووسراو وەک کۆدی MATLAB جێبەجێ دەبێت. هەر کەسێک بەکار بهێنێت دەتوانێت هەر بنووسێت، لەوانەش ئەوانەی بانگ دەکەن کە کاریگەرییان هەیە. inputـی ئاسایی تەنها لە بچووکی خۆتدا بەکار بهێنە. کاتێک خەڵکی تر دەنووسن، دەق بە 's' بخوێنەوە و خۆت بە str2double بیکە بە ژمارە، کە ئەگەر دەقەکە ژمارە نەبێت NaN (ژمارە نییە) دەدات:

Reading a number safely (two sample runs)خوێندنەوەی ژمارە بە سەلامەتی (دوو جار جێبەجێکردن)MATLABType in a terminalلە تێرمیناڵدا بینووسە
txt = input('Age: ', 's');
age = str2double(txt)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

str2double only converts; it never runs the text as code. Test the result with isnan(age) (next lesson).

str2double تەنها دەیگۆڕێت؛ هەرگیز دەقەکە وەک کۆد جێبەجێ ناکات. ئەنجامەکە بە isnan(age) بپشکنە (وانەی داهاتوو).

If you know Python: there input() always returns text and the Python lessons never evaluate it (input is text, never use eval). MATLAB's plain input evaluates — fine for a lab, but it means: type numbers, not text.

ئەگەر Python دەزانیت: لەوێ input() هەمیشە دەق دەگەڕێنێتەوە و وانەکانی Python هەرگیز هەژماری ناکەن (نێوخستە دەقە، هەرگیز eval بەکار مەهێنە). inputـی ئاسایی MATLAB هەژمار دەکات — بۆ تاقیگە باشە، بەڵام واتای ئەوەیە: ژمارە بنووسە، نەک دەق.

Quick checkپشکنینی خێراA user types twenty at txt = input('Age: ', 's'); age = str2double(txt). What is age? لە txt = input('Age: ', 's'); age = str2double(txt)ـدا دەنووسێت twenty. age چییە؟

Showing results: disp and num2strپیشاندانی ئەنجام: disp و ⁦num2str⁩

There are three ways to show a value. Typing a name without ; shows the name and the value (A = …). disp(A) shows only the value, with no name — tidier in a program. And to show text and numbers on one line, join pieces of text with square brackets [ ], after turning the number into text with num2str:

سێ ڕێگە بۆ پیشاندانی هەن. نووسینی ناوێک بێ ; ناو و پیشان دەدات (A = …). disp(A) تەنها پیشان دەدات، بێ ناو — لە ڕێکترە. بۆ پیشاندانی دەق و ژمارە لە یەک دێڕدا، پارچە دەقەکان بە کەوانەی چوارگۆشە [ ] پێکەوە ببەستە، دوای ئەوەی ژمارەکە بە num2str کردە دەق:

Three ways to show the same valueسێ ڕێگە بۆ پیشاندانی هەمان نرخMATLABType in a terminalلە تێرمیناڵدا بینووسە
A = pi*2.5^2;
A
disp(A)
disp('Circle area')
disp(['Area = ', num2str(A)])
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

The name and the value.

ناو و .

Lineدێڕی 3

Only the value.

تەنها .

Lineدێڕی 5

num2str(A) gives the text '19.635': for a number like this it keeps four digits after the decimal point and drops the last zero. [ ] joins it to 'Area = '.

num2str(A) دەقی '19.635' دەدات: بۆ ژمارەیەکی وەک ئەمە چوار ژمارەی دوای دەهێڵێتەوە و سفری کۆتایی لادەبات. [ ] دەیلکێنێت بە 'Area = 'ـەوە.

Why is num2str needed? Text in single quotes is a row of , and [ ] joins rows. A number placed inside [ ] with text is turned into the character with that code number, not into digits: ['Mark: ', 65] gives Mark: A, because 65 is the code of the letter A. disp of an empty array prints nothing at all.

بۆچی num2str پێویستە؟ دەقی ناو تاک ڕیزێکە لە و [ ] ڕیزەکان پێکەوە دەلکێنێت. ژمارەیەک کە لەگەڵ دەق لەناو [ ] دابنرێت دەبێتە ئەو کە ئەو ژمارەیە کۆدەکەیەتی، نەک ببێتە ژمارە: ['Mark: ', 65] ئەنجامی Mark: A دەدات، چونکە ⁦65⁩ کۆدی پیتی Aـیە. dispـی بەتاڵ هیچ چاپ ناکات.

Quick checkپشکنینی خێراWhat does disp(['Total: ', num2str(12)]) print?disp(['Total: ', num2str(12)]) چی چاپ دەکات؟

Showing results: disp and num2strپیشاندانی ئەنجام: disp و ⁦num2str⁩

Text in MATLAB: quotes and comparingدەق لە MATLABـدا: نیشانەکانی گوتە و بەراوردکردن

MATLAB has two kinds of text. 'Hello' in single quotes is a character vector: a whose are characters, so length('Hello') is 5. "Hello" in double quotes is a string object, which modern MATLAB recommends for larger programs. Octave treats double-quoted text differently (as characters in which \n is already a new line). So this track writes text in single quotes — it means the same thing in MATLAB and Octave. To put an apostrophe inside, type it twice: 'Aram''s mark' is the text Aram's mark.

MATLAB دوو جۆر دەقی هەیە. 'Hello' لەناو تاکدا ⁦character vector⁩ـە: کە ، بۆیە length('Hello') دەکاتە 5. "Hello" لەناو جووتدا ئۆبجێکتی stringـە، کە MATLABـی نوێ بۆ گەورەتر پێشنیاری دەکات. Octave دەقی ناو جووت بە شێوەیەکی تر مامەڵە دەکات (وەک کە \n تێیاندا پێشتر دێڕی نوێیە). بۆیە ئەم بەشە دەق لەناو تاکدا دەنووسێت — لە MATLAB و Octaveـدا هەمان واتای هەیە. بۆ دانانی ئاپۆسترۆف لەناویدا، دوو جار بینووسە: 'Aram''s mark' دەقی ⁦Aram's mark⁩ـە.

To ask whether two pieces of text are equal, use strcmp(a, b): it answers 1 (true) or 0 (false). Do not use == for text: it compares letter by letter and stops with an error when the lengths differ ('yes' == 'no'). To look for text inside text use contains (true or false) or strfind (the positions) — the old findstr is no longer recommended (more families of functions). lower and upper change the case, so strcmp(lower(answer), 'yes') accepts Yes and YES too.

بۆ پرسینی ئەوەی ئایا دوو پارچە دەق یەکسانن، strcmp(a, b) بەکار بهێنە: 1 (ڕاست) یان 0 (هەڵە) دەداتەوە. بۆ دەق == بەکار مەهێنە: پیت بە پیت بەراورد دەکات و کاتێک درێژییەکان جیاواز بن بە دەوەستێت ('yes' == 'no'). بۆ گەڕان بەدوای دەقێک لەناو دەقدا contains (ڕاست یان هەڵە) یان strfind (شوێنەکان) بەکار بهێنە — findstrـی کۆن ئیتر پێشنیار ناکرێت (خێزانی تری کردارەکان). lower و upper قەبارەی پیت دەگۆڕن، بۆیە strcmp(lower(answer), 'yes') Yes و YESـیش قبووڵ دەکات.

Comparing and searching textبەراوردکردن و گەڕان لە دەقداMATLABType in a terminalلە تێرمیناڵدا بینووسە
strcmp('Erbil', 'Erbil')
strcmp('Erbil', 'erbil')
strcmp(lower('YES'), 'yes')
contains('Sulaymaniyah', 'mani')
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Text comparison is : E and e differ.

بەراوردی دەق : E و e جیاوازن.

Quick checkپشکنینی خێراWhich test is safe for checking whether answer is the text 'yes'?کام پشکنین بۆ زانینی ئەوەی ئایا answer دەقی 'yes'ـە سەلامەتە؟

Printing with fprintfچاپکردن بە fprintf

fprintf gives you full control over a line of output. Its first is a format: text with gaps. Each gap is a that starts with % and says how to write one value; the values follow, separated by commas. Unlike disp, fprintf never starts a new line by itself: you end the line with \n.

fprintf کۆنترۆڵی تەواوت دەداتێ بەسەر دێڕێکی . یەکەم فۆرماتێکە: دەقێک کە چەند شوێنێکی بەتاڵی تێدایە. هەر شوێنێکی بەتاڵ کە بە % دەست پێدەکات و دەڵێت یەک چۆن بنووسرێت؛ بە کۆما جیاکراوە دوای ئەو دێن. بە پێچەوانەی disp، fprintf هەرگیز خۆی دێڕی نوێ دەست پێناکات: دێڕەکە بە \n کۆتایی پێدەهێنیت.

The line fprintf('Area = %8.2f cm^2n', A) split into coloured parts: the text 'Area = ' is printed as it is; %8.2f is a gap for a number, at least 8 characters wide with 2 decimals, f meaning fixed point; ' cm^2' is more text; n is a new line; A is the value that fills the gap. The output is Area = 19.63 cm^2, where 19.63 is padded with three spaces to 8 characters.دێڕی fprintf('Area = %8.2f cm^2n', A) بە بەشی ڕەنگاوڕەنگ: دەقی 'Area = ' وەک خۆی چاپ دەکرێت؛ %8.2f شوێنە بەتاڵەکەیە بۆ ژمارەیەک بە پانیی لانیکەم هەشت نووسە و دوو ژمارە دوای فاریزە؛ ' cm^2' دەقێکی ترە؛ n دێڕی نوێیە؛ A ئەو نرخەیە کە شوێنە بەتاڵەکە پڕ دەکاتەوە. دەرخستەکە دەبێتە Area = 19.63 cm^2، کە 19.63 بە سێ بۆشایی پڕ دەکرێتەوە تا ببێتە هەشت نووسە.
The parts of an fprintf formatبەشەکانی فۆرماتی fprintfCêIT
Conversion codes (the commands list names %s %d %f %e %g) (لیستی %⁦s %d %f %e %g⁩ ناو دەبات)
CodeکۆدWritesچی دەنووسێتExampleنموونەOutput
%da whole numberfprintf('%d\n', 42)42
%fa number with 6 decimals (fixed point)ژمارەیەک بە شەش ژمارەی دوای fprintf('%f\n', pi)3.141593
%.2fthe same with 2 decimalsهەمان شت بە دوو ژمارەی دوای fprintf('%.2f\n', pi)3.14
%efprintf('%e\n', 123456)1.234560e+05
%gthe shorter of %f and %e, no extra zerosکورتترینی %f و %e، بێ سفری زیادەfprintf('%g\n', 1234567)1.23457e+06
%stextدەقfprintf('%s has %d marks\n', 'Hana', 3)Hana has 3 marks
%cone characterیەک fprintf('%c\n', 'K')K
Special characters in a format لە فۆرماتدا
SpecialتایبەتMeaningواتا
\nnew lineدێڕی نوێ
\ttab (jump to the next column)تاب (بازدان بۆ ستوونی دواتر)
%%a percent signنیشانەی سەدی
''a single quote inside the format تاک لەناو فۆرماتەکەدا
\\a backslashباکسلاش
Percent sign and tabsنیشانەی سەدی و تابMATLABType in a terminalلە تێرمیناڵدا بینووسە
fprintf('Pass rate: %d%%\n', 85)
fprintf('Name\tMark\n')
fprintf('Lana\t%d\n', 91)
Quick checkپشکنینی خێراWhat does fprintf('%.1f kg\n', 72.46) print?fprintf('%.1f kg\n', 72.46) چی چاپ دەکات؟

Printing with fprintfچاپکردن بە fprintf

Width, precision and flagsپانی، وردی و نیشانەکان

Between % and the letter you can add a width and a precision: %8.2f means at least 8 characters wide, 2 decimals. A number shorter than the width is padded with spaces on the left, so numbers in several lines line up on the right. Flags change the padding: - pads on the right (left-justified), + always shows the sign, 0 pads with zeros. The square brackets below only show where each field starts and ends:

لە نێوان % و پیتەکەدا دەتوانیت پانی و وردی زیاد بکەیت: %8.2f واتە پانیی لانیکەم هەشت ، دوو ژمارە دوای . ژمارەیەک کە لە پانییەکە کورتتر بێت لە لای چەپەوە بە پڕ دەکرێتەوە، بۆیە ژمارەکانی چەند دێڕێک لە لای ڕاستەوە ڕێک دەبنەوە. نیشانەکان شێوەی پڕکردنەوە دەگۆڕن: - لە لای ڕاستەوە پڕ دەکاتەوە (ڕێکخستن بە لای چەپدا)، + هەمیشە نیشانەی ژمارە پیشان دەدات، 0 بە سفر پڕ دەکاتەوە. کەوانە چوارگۆشەکانی خوارەوە تەنها پیشان دەدەن هەر خانەیەک لە کوێ دەست پێدەکات و لە کوێ کۆتایی دێت:

Width and flagsپانی و نیشانەکانMATLABType in a terminalلە تێرمیناڵدا بینووسە
fprintf('[%6.2f]\n', pi)
fprintf('[%-6.2f]\n', pi)
fprintf('[%+d]\n', 5)
fprintf('[%05d]\n', 42)
fprintf('[%8s]\n', 'Erbil')
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

6 characters: two spaces, then 3.14.

شەش : دوو ، پاشان ⁦3.14⁩.

Lineدێڕی 5

Width works for text too: Erbil is padded to 8 characters.

پانی بۆ دەقیش کار دەکات: Erbil تا هەشت پڕ دەکرێتەوە.

Quick checkپشکنینی خێراHow many spaces does fprintf('[%7.3f]\n', 2.5) put before the number?fprintf('[%7.3f]\n', 2.5) چەند پێش ژمارەکە دادەنێت؟

Printing with fprintfچاپکردن بە fprintf

The format repeats over a vectorفۆرمات بۆ هەموو توخمەکانی ئاڕاستەبڕێک دووبارە دەبێتەوە

If you give fprintf more values than gaps, it uses the format again for the next values, until all are used. MathWorks' page says the format is applied to all elements of the arrays, in column order. That gives you a whole table in one line. Put the columns of your table as the rows of a matrix: MATLAB reads a matrix column by column, so each column of [C; F] (one Celsius value and its Fahrenheit value) fills one line.

ئەگەر لە شوێنە بەتاڵەکان زیاتر بدەیت بە fprintf، فۆرماتەکە دووبارە بەکار دەهێنێتەوە بۆ دواتر، تا هەموویان بەکار دێن. پەڕەی MathWorks دەڵێت فۆرماتەکە بەسەر هەموو جێبەجێ دەکرێت، ستوون بە ستوون. بەمە خشتەیەکی تەواو لە یەک دێڕدا دەنووسیت. ستوونەکانی خشتەکەت وەک ڕیزەکانی دابنێ: MATLAB ستوون بە ستوون دەخوێنێتەوە، بۆیە هەر ستوونێکی [C; F] (یەک پلەی سیلیزی و پلەی فەهرەنهایتی بەرامبەری) یەک دێڕ پڕ دەکاتەوە.

A conversion table in one fprintfخشتەیەکی گۆڕین بە یەک fprintfMATLABType in a terminalلە تێرمیناڵدا بینووسە
C = [0 10 20 30 40];
F = C*1.8 + 32;
fprintf('%3d C = %5.1f F\n', [C; F])
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Multiplying a vector by a number multiplies every element (a number with an array).

لێکدانی لە ژمارەیەک هەموو لێک دەدات (ژمارەیەک لەگەڵ ڕیزەکێک).

Lineدێڕی 3

[C; F] is a 2×52 \times 5 matrix (joining); its 10 numbers are used two at a time, column by column.

[C; F] 2×52 \times 5ـە (پێکەوەلکاندن)؛ دە ژمارەکەی دوو دوو بەکار دێن، ستوون بە ستوون.

A short list on one lineلیستێکی کورت لە یەک دێڕداMATLABType in a terminalلە تێرمیناڵدا بینووسە
fprintf('%d ', [4 9 -1])
fprintf('\n')
Quick checkپشکنینی خێراWhat does fprintf('%d-%d\n', [1 2 3 4]) print?fprintf('%d-%d\n', [1 2 3 4]) چی چاپ دەکات؟

Printing with fprintfچاپکردن بە fprintf

More display optionsهەڵبژاردەی تری پیشاندان

The format command changes how MATLAB shows results that you do not print yourself; display formats covers short, long, shortE, bank, rational and the rest. The commands list names three more options:

format شێوەی پیشاندانی ئەو ئەنجامانە دەگۆڕێت کە خۆت چاپیان ناکەیت؛ شێوەکانی پیشاندان short، long، shortE، bank، rational و ئەوانی تر ڕوون دەکاتەوە. لیستی سێ هەڵبژاردەی تر ناو دەبات:

Spacing and sign displayدێڕی بەتاڵ و پیشاندانی نیشانە
CommandEffectکاریگەری
format compactLeaves out the blank lines around each result — the look used in this track's outputs.دێڕە بەتاڵەکانی دەوری هەر ئەنجامێک لادەبات — ئەو شێوەیەی لە ئەم بەشەدا بەکارهاتووە.
format loosePuts the blank lines back (MATLAB's default spacing).دێڕە بەتاڵەکان دەگەڕێنێتەوە (شێوەی بنەڕەتیی MATLAB).
format +Shows only the sign of each element: +, -, or a blank for zero. Handy for seeing the pattern of signs in a big matrix.تەنها نیشانەی هەر پیشان دەدات: +، -، یان بۆ سفر. بۆ بینینی نەخشەی نیشانەکان لە گەورەدا بەسوودە.

None of these changes a stored value, and format default brings everything back. fprintf is not affected by format at all: its codes decide the look.

هیچ یەکێک لەمانە هەڵگیراو ناگۆڕێت و format default هەموو شتێک دەگەڕێنێتەوە. fprintf هیچ کاریگەرییەکی formatـی لەسەر نییە: کۆدەکانی خۆی شێوەکە دیاری دەکەن.

Quick checkپشکنینی خێراAfter format compact, what changes?دوای format compact چی دەگۆڕێت؟

A whole program: input, processing, outputبەرنامەیەکی تەواو: نێوخستە، پرۆسێس، دەرخستە

Now put the pieces together. Almost every program takes input, processes it and gives output: here the input is a radius, the processing is two formulas, and the output is one tidy line. Planning first — what comes in, what is computed, what goes out — is the project method of the shared lessons.

ئێستا پارچەکان پێکەوە دابنێ. نزیکەی هەموو نێوخستە وەردەگرێت، پرۆسێسی دەکات و دەرخستە دەدات: لێرەدا نیوەتیرەیەکە، پرۆسێسەکە بریتییە لە دوو و یەک دێڕی ڕێکوپێکە. پلاندانان لە پێشدا — چی دێتە ژوورەوە، چی هەژمار دەکرێت، چی دەچێتە دەرەوە — ڕێگای پڕۆژەی وانە هاوبەشەکانە.

circle.m with input and fprintf (a sample run)⁦circle.m⁩ بە input و fprintf (جارێک جێبەجێکردن)circle.mMATLABType in a terminalلە تێرمیناڵدا بینووسە
% circle.m - area and circumference of a circle
r = input('Radius in cm: ');
area = pi*r^2;
circ = 2*pi*r;
fprintf('Area = %.2f cm^2, circumference = %.2f cm\n', area, circ)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Input: the user types the radius (a number, so no 's').

: نیوەتیرە دەنووسێت (ژمارەیە، بۆیە 's' نییە).

Lineدێڕی 3-4

Processing: two formulas, silent because of the semicolons.

پرۆسێس: دوو ، بێدەنگ بەهۆی .

Lineدێڕی 5

Output: two gaps, two values, one line ending with \n.

: دوو شوێنی بەتاڵ، دوو ، یەک دێڕ کە بە \n کۆتایی دێت.

Watch a slightly shorter version of the same script run line by line. Notice the order: nothing is computed until you answer the prompt, and the Workspace fills up as the lines run.

سەیری وەشانێکی کەمێک کورتتری هەمان بکە کە دێڕ بە دێڕ جێبەجێ دەبێت. سەرنجی ڕیزبەندییەکە بدە: تا وەڵامی نەدەیتەوە هیچ هەژمار ناکرێت و بە جێبەجێبوونی دێڕەکان پڕ دەبێت.

Animation in six frames. Left: the Editor with circle.m (a comment line, r = input('Radius in cm: ');, area = pir^2;, circ = 2pi*r; and an fprintf line). Right: the Command Window and the Workspace. 1: >> circle is typed. 2: the input line is highlighted and the prompt Radius in cm: waits. 3: 2.5 is typed and r = 2.5000 appears in the Workspace. 4: the area line runs and area = 19.6350 appears. 5: the circ line runs and circ = 15.7080 appears. 6: the fprintf line prints Area = 19.63 cm^2 and the >> prompt returns.
Animation in six frames. Left: the Editor with circle.m (a comment line, r = input('Radius in cm: ');, area = pir^2;, circ = 2pi*r; and an fprintf line). Right: the Command Window and the Workspace. 1: >> circle is typed. 2: the input line is highlighted and the prompt Radius in cm: waits. 3: 2.5 is typed and r = 2.5000 appears in the Workspace. 4: the area line runs and area = 19.6350 appears. 5: the circ line runs and circ = 15.7080 appears. 6: the fprintf line prints Area = 19.63 cm^2 and the >> prompt returns.
circle.m running line by line⁦circle.m⁩ دێڕ بە دێڕ جێبەجێ دەبێتCêIT
Quick checkپشکنینی خێراIn circle.m, which line is the processing part?لە ⁦circle.m⁩ـدا کام دێڕ بەشی پرۆسێسـە؟

When a script stopsکاتێک دەسنووسێک دەوەستێت

If a line has a mistake, MATLAB stops at that line and prints a red that names the problem and the line, for example Error in circle (line 5). The lines before it have already run, so their variables are in the workspace — useful when you look for the cause. Here the last line of circle.m was typed as ferintf (a typo the course's Kurdish guide also has):

ئەگەر دێڕێک هەڵەی تێدا بێت، MATLAB لەو دێڕەدا دەوەستێت و سوور چاپ دەکات کە کێشەکە و دێڕەکە ناو دەبات، بۆ نموونە ⁦Error in circle (line 5)⁩. دێڕەکانی پێش ئەو پێشتر جێبەجێ بوون، بۆیە لە — کاتێک بەدوای هۆکارەکەدا دەگەڕێیت بەسوودە. لێرەدا دوایین دێڕی ⁦circle.m⁩ بە ferintf نووسراوە (هەڵەیەکی نووسین کە ڕێبەرە کوردییەکەی وانەکەش هەیەتی):

A typo in the last lineهەڵەیەکی نووسین لە دوایین دێڕداMATLABType in a terminalلە تێرمیناڵدا بینووسە
ferintf('Area = %.2f cm^2\n', area)

A script can also seem to hang: it waits at an input prompt you did not notice, or a loop never ends. Press Ctrl+C in the Command Window to stop it — a to remember. Then fix the cause and run again.

دەشێت وا دەربکەوێت کە گیرابێت: لە دوای inputـێک چاوەڕێ دەکات کە سەرنجت نەداوە، یان خولێک هەرگیز کۆتایی نایەت. لە ⁦Command Window⁩ ⁦Ctrl+C⁩ دابگرە بۆ وەستاندنی — کە دەبێت لە بیرت بێت. پاشان هۆکارەکە چاک بکە و دووبارە جێبەجێی بکەرەوە.

Quick checkپشکنینی خێراMATLAB reports an error in line 5 of a 6-line script. Which lines have run?MATLAB لە دێڕی پێنجی شەش دێڕیدا ڕادەگەیەنێت. کام دێڕانە جێبەجێ بوون؟

Common mistakesهەڵە باوەکان

MistakeهەڵەWhat happensچی ڕوودەداتFixچارەسەر
A script named like a variable or built-in function (sum.m, input.m, a script x.m while x is a variable) بە ناوی یان (sum.m، input.m، بە ناوی x.m کاتێک x )The built-in function is hidden, or typing the name shows the variable instead of running the script. دەشاردرێتەوە، یان نووسینی ناوەکە لە جیاتی جێبەجێکردنی پیشان دەدات.Choose a clear, unused name: circle_area.m. Check with exist name.ناوێکی ڕوون و بەکارنەهاتوو هەڵبژێرە: circle_area.m. بە exist name بیپشکنە.
Spaces or - in the file name (lab 2.m, lab-2.m) یان - لە ناوی (lab 2.m، lab-2.m)The script cannot be run by its name. بە ناوەکەی جێبەجێ ناکرێت.Use letters, digits and _: lab_2.m.پیت، ژمارە و _ بەکار بهێنە: lab_2.m.
Forgetting \n at the end of an fprintf formatلەبیرچوونی \n لە کۆتایی فۆرماتی fprintfThe next output or the >> prompt sticks to the end of the line: Area = 19.63>> دواتر یان >> بە کۆتایی دێڕەکەوە دەنووسێت: Area = 19.63>> End every line with \n.هەموو دێڕێک بە \n کۆتایی پێبهێنە.
input without 's' for a nameinput بێ 's' بۆ ناوMATLAB tries to evaluate the name, shows an error and asks again.MATLAB هەوڵ دەدات ناوەکە هەژمار بکات، پیشان دەدات و دووبارە دەپرسێتەوە.name = input('Name: ', 's')name = input('Name: ', 's')
ferintf, fprint, Disp (typos and capitals)ferintf، fprint، Disp (هەڵەی نووسین و پیتی گەورە)Unrecognized function or variable and the script stops.⁦Unrecognized function or variable⁩ و دەوەستێت.Type fprintf, disp exactly, in lower case.fprintf و disp ڕێک و بە پیتی بچووک بنووسە.
A number inside [ ] with text: ['Age: ', 19]ژمارەیەک لەگەڵ دەق لەناو [ ]: ['Age: ', 19]A strange character instead of the digits. سەیر لە جیاتی ژمارەکان.['Age: ', num2str(19)] or fprintf('Age: %d\n', 19)['Age: ', num2str(19)] یان fprintf('Age: %d\n', 19)
Comparing text with ==بەراوردکردنی دەق بە ==An error when the lengths differ. کاتێک درێژییەکان جیاوازن.strcmp(a, b)strcmp(a, b)
Using %d for a number with decimalsبەکارهێنانی %d بۆ MATLAB switches to another format, such as 2.500000e+00.MATLAB دەچێتە سەر فۆرماتێکی تر، وەک 2.500000e+00.%f, %.2f or %g%f، %.2f یان %g

Practiceڕاهێنان

ExerciseڕاهێنانTemperature converterگۆڕەری پلەی گەرمی

Write a script temperature.m that asks for a temperature in Celsius and prints it in Fahrenheit with one decimal (F=1.8C+32F = 1.8C + 32). Test it with 37.5.

بە ناوی temperature.m بنووسە کە پلەی گەرمی بە سیلیزی دەپرسێت و بە فەهرەنهایت بە یەک ژمارەی دوای چاپی دەکات (F=1.8C+32F = 1.8C + 32). بە ⁦37.5⁩ تاقیی بکەرەوە.

Hintئاماژە

A number is read with plain input; one fprintf with two gaps prints both values.

ژمارە بە inputـی ئاسایی دەخوێندرێتەوە؛ یەک fprintf بە دوو شوێنی بەتاڵ هەردوو چاپ دەکات.

Show answerوەڵامەکە پیشان بدە
temperature.mMATLABType in a terminalلە تێرمیناڵدا بینووسە
% temperature.m - Celsius to Fahrenheit
c = input('Temperature in C: ');
f = 1.8*c + 32;
fprintf('%.1f C is %.1f F\n', c, f)
ExerciseڕاهێنانGreetingسڵاوکردن

Ask for a name (text) and a year of birth (a number), then print Hello Karwan! In 2026 you turn 20. with the user's own name and age.

ناوێک (دەق) و ساڵی لەدایکبوون (ژمارە) بپرسە، پاشان بە ناو و تەمەنی خودی چاپ بکە: Hello Karwan! In 2026 you turn 20.

Hintئاماژە

The name needs 's'; the year does not. %s writes text.

ناوەکە پێویستی بە 's'ـە؛ ساڵەکە نا. %s دەق دەنووسێت.

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
name = input('Your name: ', 's');
year = input('Year of birth: ');
fprintf('Hello %s! In 2026 you turn %d.\n', name, 2026 - year)
ExerciseڕاهێنانPredict the outputدەرخستە پێشبینی بکە

Without MATLAB, write down exactly what these three lines print: fprintf('%d-%d\n', [1 2 3 4]) fprintf('%5.1f|\n', 2.36) fprintf('%s\n', 'OK')

بێ MATLAB، ڕێک ئەوە بنووسە کە ئەم سێ دێڕە چاپی دەکەن: fprintf('%d-%d\n', [1 2 3 4]) fprintf('%5.1f|\n', 2.36) fprintf('%s\n', 'OK')

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
fprintf('%d-%d\n', [1 2 3 4])
fprintf('%5.1f|\n', 2.36)
fprintf('%s\n', 'OK')
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

The format is used twice.

فۆرماتەکە دوو جار بەکار دێت.

Lineدێڕی 2

2.4 has 3 characters, so two spaces make the width 5.

2.4 سێ ، بۆیە دوو پانییەکە دەکەن بە پێنج.

ExerciseڕاهێنانA tea-house receiptپسوولەی چایخانە

Aram buys 3 teas at 1500 IQD and 4 kulichas at 750 IQD. Print a receipt with the item name left-justified in 8 characters, the quantity in 3, the price in 5 and the line total in 6, then the total. Use one fprintf per line.

ئارام سێ چا بە ⁦1500⁩ دینار و چوار کولێچە بە ⁦750⁩ دینار دەکڕێت. پسوولەیەک چاپ بکە کە ناوی شتەکە لە هەشت بە لای چەپدا ڕێک بخات، ژمارەکە لە سێ، نرخەکە لە پێنج و کۆی دێڕەکە لە شەشدا، پاشان کۆی گشتی. بۆ هەر دێڕێک یەک fprintf بەکار بهێنە.

Hintئاماژە

%-8s = text, left-justified, 8 wide. Count the characters of one finished line to place the total.

%-8s = دەق، بە لای چەپدا ڕێکخراو، پانیی هەشت. دێڕێکی تەواو بژمێرە بۆ دانانی کۆی گشتی.

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
fprintf('%-8s %3d x %5d = %6d IQD\n', 'Tea', 3, 1500, 3*1500)
fprintf('%-8s %3d x %5d = %6d IQD\n', 'Kulicha', 4, 750, 4*750)
fprintf('%-8s %20d IQD\n', 'Total', 3*1500 + 4*750)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1-2

%-8s pads the name on the right, so the numbers line up.

%-8s ناوەکە لە لای ڕاستەوە پڕ دەکاتەوە، بۆیە ژمارەکان ڕێک دەبنەوە.

Lineدێڕی 3

%20d makes the total end in the same column as the line totals.

%20d وا دەکات کۆی گشتی لە هەمان ستووندا کۆتایی بێت.

ExerciseڕاهێنانFix the scriptدەسنووسەکە چاک بکەرەوە

This script should ask for a student's name and mark and print one line. Find the four mistakes: name = input('Name: '); mark = input('Mark: ') ferintf('%s got %d\n' name, mark) Disp('Done')

ئەم دەبێت ناو و نمرەی قوتابییەک بپرسێت و یەک دێڕ چاپ بکات. چوار هەڵەکە بدۆزەرەوە: name = input('Name: '); mark = input('Mark: ') ferintf('%s got %d\n' name, mark) Disp('Done')

Show answerوەڵامەکە پیشان بدە
  • Line 1: a name is text, so add 's'.
  • Line 2: no mistake for MATLAB, but without ; it echoes mark = …; add ; for a tidy output.
  • Line 3: ferintf → fprintf, and a comma is missing after the format.
  • Line 4: Disp → disp (MATLAB is case-sensitive).
  • دێڕی 1: ناو دەقە، بۆیە 's' زیاد بکە.
  • دێڕی 2: بۆ MATLAB هەڵە نییە، بەڵام بێ ; دەنووسێتەوە mark = …؛ بۆ ڕێک ; زیاد بکە.
  • دێڕی 3: ferintf بکە بە fprintf، هەروەها کۆمایەک دوای فۆرماتەکە کەمە.
  • دێڕی 4: Disp بکە بە disp (MATLAB ).
report.mMATLABType in a terminalلە تێرمیناڵدا بینووسە
name = input('Name: ', 's');
mark = input('Mark: ');
fprintf('%s got %d\n', name, mark)
disp('Done')
ExerciseڕاهێنانFiles from the Command Windowفایلەکان لە ⁦Command Window⁩ـەوە

Make a folder labs, go into it, create x = 5 and y = 12, save only y in week1.mat, clear the workspace, load the file and show what is in the workspace.

بە ناوی labs دروست بکە، بچۆ ناوی، x = 5 و y = 12 دروست بکە، تەنها y لە week1.matـدا هەڵبگرە، پاک بکەرەوە، بار بکەرەوە و پیشان بدە چی لە .

Hintئاماژە

save file.mat name keeps only the variables you list; who lists the names in the workspace.

save file.mat name تەنها ئەو هەڵدەگرێت کە ناویان دەنووسیت؛ who ناوەکانی ناو لیست دەکات.

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
mkdir labs
cd labs
x = 5; y = 12;
save week1.mat y
clear
load week1.mat
who

Summaryکورتە

  • A script is a .m text file of commands that run top to bottom; create it with New Script or edit name, run it by typing its name or pressing Run.
  • Script variables stay in the shared Workspace; file names follow the variable-name rules and must not reuse built-in names.
  • pwd, cd, dir, what, type, mkdir, copyfile, movefile, delete, save/load, diary manage files from MATLAB.
  • input(prompt) evaluates what is typed; input(prompt, 's') keeps text; Enter alone gives an empty result.
  • disp shows a value without its name; join text and numbers with [ ] and num2str.
  • fprintf uses %d %f %e %g %s %c, width and precision (%8.2f), \n, \t, %%, and repeats its format over all values.
  • Write text in single quotes; compare text with strcmp, search it with contains/strfind.
  • .mـە کە کۆمەڵێک هەڵدەگرێت؛ ئەو لە سەرەوە بۆ خوارەوە جێبەجێ دەبن؛ بە ⁦New Script⁩ یان edit name دروستی بکە و بە نووسینی ناوەکەی یان داگرتنی Run جێبەجێی بکە.
  • لە هاوبەشدا دەمێننەوە؛ ناوی یاساکانی ناوی دەگرنە بەر و نابێت ناوی دووبارە بکەنەوە.
  • pwd، cd، dir، what، type، mkdir، copyfile، movefile، delete، save/load و diary لەناو MATLABـەوە بەڕێوە دەبەن.
  • input(prompt) ئەوەی نووسراوە هەژمار دەکات؛ input(prompt, 's') دەق دەهێڵێتەوە؛ تەنها Enter ئەنجامێکی بەتاڵ دەدات.
  • disp بێ ناوەکەی پیشان دەدات؛ دەق و ژمارە بە [ ] و num2str پێکەوە ببەستە.
  • fprintf کۆدەکانی %d %f %e %g %s %c، پانی و وردی (%8.2f)، \n، \t و %% بەکار دەهێنێت و فۆرماتەکەی بۆ هەموو دووبارە دەکاتەوە.
  • دەق لەناو تاکدا بنووسە؛ دەق بە strcmp بەراورد بکە و بە contains/strfind تێیدا بگەڕێ.
Review questionsپرسیارەکانی پێداچوونەوە
Why does input('Name: ') fail when you type Shilan?بۆچی input('Name: ') سەرناکەوێت کاتێک Shilan دەنووسیت؟

It evaluates the answer, so MATLAB looks for a variable or function called Shilan. Add 's' to keep the text.

وەڵامەکە هەژمار دەکات، بۆیە MATLAB بەدوای یان بە ناوی Shilanدا دەگەڕێت. بۆ هێشتنەوەی دەقەکە 's' زیاد بکە.

What is the difference between disp(x) and fprintf('%g\n', x)?جیاوازیی disp(x) و fprintf('%g\n', x) چییە؟

disp uses MATLAB's display format; fprintf writes exactly what the format says and needs \n for a new line.

disp شێوەی پیشاندانی MATLAB بەکار دەهێنێت؛ fprintf ڕێک ئەوە دەنووسێت کە فۆرماتەکە دەیڵێت و بۆ دێڕی نوێ پێویستی بە \nـە.

A script uses r but never sets it. What can go wrong?دەسنووسێک r بەکار دەهێنێت بەڵام هەرگیز نرخ نادات بە r. چی هەڵە دەچێت؟

It silently uses an old r left in the shared Workspace, or stops with an error if there is none.

بە بێدەنگی rـێکی کۆن بەکار دەهێنێت کە لە هاوبەشدا ماوەتەوە، یان ئەگەر نەبێت بە دەوەستێت.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Making Decisions: Relational Operators and if / elseif / elseبڕیاردان: کارپێکەرەکانی بەراوردکردن و ⁦if / elseif / else⁩

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.