2017-12-02 22 views
1

私は大学のMATLABでコードを書いており、インデントが重要であると言われており、それによって8つのうち3つが失われてしまいました。インデントは「スタイル」だけではないのですか?MATLABでのインデントが重要ですか?

コードは次のよう場合:

function[pp,pb,pd]=demopoly(fname,N) 

%The function reads the data from the file and computes the coefficients of a polynomial of degree N of three polynomials pp, pb, and pd that best fit the population, births and deaths in the data. 
%The function returns the three variables pp, pb and pd in this order. 

C=csvread(fname); 

pp=polyfit(C(:,1),C(:,2),N); 

pb=polyfit(C(:,1),C(:,3),N); 

pd=polyfit(C(:,1),C(:,4),N); 

end 

を誰かがくぼみが発生している必要がありますどこに私が知っていることはできますか?他のスペクトルに

+0

https://uk.mathworks.com/help/matlab/matlab_prog/case-and-space-sensitivity.html:あなたの関数(と少しの最適化) 。]配列を定義するときを除いて、空白に鈍感です。* "あなたのコードが読みにくいため、あなたがマークダウンしたと思われます。 –

+0

スタイリングだけのためのインデントは非常に重要です。読みやすさの重要性を過小評価しないでください。 – Carcigenicate

+1

何のために重要ですか?コードの実行方法に違いはありませんが、適切なインデント、コメントなどを使用してコードの書式を適切に設定すると、自分以外の人が読むのがはるかに簡単になり、日々の帰りに読みやすくなります、数ヶ月、それを見ない年。 – ImaginaryHuman072889

答えて

2

インデントは私が離れてだけはCtrl +はCtrl +です。難しい?なぜインデントが重要であるか他の答えが指摘している。インデントが発生するはずの問題については、のコードにインデントの問題はありません。です。

私はあなたが示したコードのためのあなたのマークをカットしなければならなかった場合、私はのためにカットします:その最初のコメントには、改行を持たない

  • 。それは長すぎるため、スクロールして読む必要があります。
  • コード内に余分な改行があります。
5

nomatterssuchaspunctuationandindentationarenotjustmattersofstyletheyareanessentialaidtohumancomprehensionofwrittentextwhetherprosepoetryorcodeicouldgoonbutwont

4

がoverdoersある

No, matters such as 

     punctuation 
         and indentation 

             are not just matters of style 

                 they are an essential aid to 

             human comprehension of 

             written text whether prose 
         poetry or 

     code 

     i could go on 

but wont 
2

Matlabのコードは、(例えば、Pythonとは異なり)インデンデーションに敏感ではありませんが、それはコードの可読性を改善するためにインデントを使用することが非常に重要だとそれがmaintainabilityです。

コードを書き留める際に手動でインデントするのが面倒な場合は、完了した後にファイルに適用できるSmart Indent関数を提供します(詳細はhere)。

files = dir(fullfile(folder,'*.m')); 

for i = 1:numel(files) 
    file_name = files(i).name; 
    file_path = fullfile(folder,file_name); 

    file_handle = matlab.desktop.editor.openDocument(file_path); 
    file_handle.smartIndentContents() 
    file_handle.save() 
    file_handle.close() 
end 

これは私がフォーマットする方法をされています。あなたもCTRL+ACTRL+Iのためにあまりにも怠惰されている場合は、特定のフォルダ内にあるすべての.mファイルへSmart Indentを適用し、小さな「狂気のバッチスクリプト」を書くことができます* MATLABコードは[ある」.. -

% The function reads the data from the file and computes 
% the coefficients of a polynomial of degree N of three 
% polynomials (pp, pb, and pd) that best fit the population, 
% births and deaths in the data. 

% The function returns the three variables pp, pb and pd 
% in this order. 

function [pp,pb,pd] = demopoly(fname,N) 

    C = csvread(fname); 
    C_1 = C(:,1); 

    pp = polyfit(C_1,C(:,2),N); 
    pb = polyfit(C_1,C(:,3),N); 
    pd = polyfit(C_1,C(:,4),N); 

end 
+1

コメントは関数ヘッダーの下に置かなければなりません。そのため、コメントは 'help demopoly'コマンドによって取り上げられます。 –

+0

さて、私は通常、このアプローチを使用して、関数自体の入力引数と出力引数にヒントを与えます。それはかなり良いです。実際のコメントを実行したい場合は、それらを関数の中に入れなければなりません!ありがとう! –

関連する問題