Can you use cell or struct as function input?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Or can you use cell or struct as function output?
Thanks!
採用された回答
Geoff
2012 年 7 月 4 日
Yes, and yes.
I'm puzzled that you're asking this here when you could quite easily test for yourself... Part of the process of coding is trying things out. Have faith that this process usually does not break your computer.
function [output] = AnswerMyQuestion( input )
output = input;
end
% Can I pass structs in and out of functions?
result = AnswerMyQuestion( struct('astring', 'value', 'anumber', 1) ) );
disp(class(result));
% Can I pass cells in and out of functions?
result = AnswerMyQuestion( cell(3,3) );
disp(class(result));
7 件のコメント
Exactly. And of course this is explained exhaustively in the documentation. E.g. there are a lot of commands as strcmp, cell2struct and struct2cell (!), which accept cells already.
Haozi
2012 年 7 月 4 日
Thank you so much!
Haozi
2012 年 7 月 4 日
plus: I guess my question is, seems that when you define the function, you don't need to define its input argument as struct, you just use a struct as the input. I don't quite understand that.
Geoff
2012 年 7 月 4 日
That's right. MATLAB is implicitly typed, so any data type is fair game for an identifier. You can reassign a different type at any stage. You can also accidentally overload inbuilt functions by assigning to a variable with the same name, so you have to be a little thoughtful. Perhaps you're accustomed to using explicitly typed languages such as Java or C, hence the confusion. It's just a different programming paradigm, with its own pros and cons.
Jan
2012 年 7 月 5 日
@Haozi: These topic belong to the general basics of Matlab and the forum is not the right location to discuss about them. Please read the "Getting Started" chapters of the documentation.
BGranato
2018 年 6 月 26 日
I actually have the same question. I have a function and I'm trying to call it with an input variable of type struct and it won't allow me.
PS: I thought all questions were welcome...?
Jan
2018 年 6 月 26 日
@BGranato: Please open a new thread and post the details there: Which function? Which input? Which error message?
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Cell Arrays についてさらに検索
タグ
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
