# How to get all data from m-function?

조회 수: 9(최근 30일)
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
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=A;
How to get all 102 values ​​that are read and counted in my function?
##### 댓글 수: 1표시숨기기 없음
Stephen23 2021년 6월 10일
Best solution: use a structure.

댓글을 달려면 로그인하십시오.

### 채택된 답변

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:
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:
A = contents.A;
...

댓글을 달려면 로그인하십시오.

### 추가 답변(1개)

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
x will be A, y will be A1, and so on

댓글을 달려면 로그인하십시오.

R2020a

### Community Treasure Hunt

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

Start Hunting!