How to get all data from m-function?

3 ビュー (過去 30 日間)
Lev Mihailov
Lev Mihailov 2021 年 6 月 10 日
コメント済み: Stephen23 2021 年 6 月 10 日
Hello! I created a function for reading dpv files, it contains many parameters that I receive. But in the example of creating a function, there is only one
function A=ReadDPV(fileName) % When reading a file, 102 values ​​come out
A=0.24*c
A1=0.1*f
B=1.2*b
...
Z2=2.4
end
When I try to read with this function, I only get the value A
X=ReadDPV(fileName)
% X=A;
How to get all 102 values ​​that are read and counted in my function?
  1 件のコメント
Stephen23
Stephen23 2021 年 6 月 10 日
Best solution: use a structure.

サインインしてコメントする。

採用された回答

Max Heiken
Max Heiken 2021 年 6 月 10 日
Hello! The value A is the only one that gets output because that is what you specified in your function declaration. You could instead specify a list of all outputs:
function [A, A2, B, .., Z2] = ReadDPV(fileName)
...
end
But this has its own drawbacks, because you would also need to accept all those values when you call the function:
[A, A2, B, .., Z2] = ReadDPV(fileName);
A perhaps better solution would be to store all these values in one container. If they are of the same type, a vector would work; if you want to still reference them by their name, a struct comes to mind; otherwise a cell array. An example for the struct approach:
function contents = ReadDPV(fileName)
contents = struct();
contents.A=0.24*c
contents.A1=0.1*f
contents.B=1.2*b
...
contents.Z2=2.4
end
And accessing them:
contents = ReadDPV(fileName);
A = contents.A;
...

その他の回答 (1 件)

Scott MacKenzie
Scott MacKenzie 2021 年 6 月 10 日
編集済み: Scott MacKenzie 2021 年 6 月 10 日
Your function is defined to only return A (which is perhaps a scalar). To return multiples values, return them as elements in a vector and define your function accordingly:
function [A, A1, B, ... ] = ReadDPV(fileName)
A = ...
A1 = ...
...
end
Then, use your function thus:
[x, y, ...] = ReadDPV(...)
x will be A, y will be A1, and so on

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by